iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection

标签 iphone objective-c ios

我正在努力将 POST 数据发送到服务器并接收到正确的响应。我从使用 setHTTPBody 开始,但是当我没有得到对某些 POST 数据的正确服务器响应并且我读到 setHTTPBody 在早期的 iO​​S 版本上有内存泄漏时,我转向了 setHTTPBodyStream。问题是 setHTTPBodyStream 导致错误 - “操作无法完成。连接由对等方重置”。

代码如下:

NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"secret but correct url goes here"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
        [request setHTTPMethod: @"POST"];
        [request setHTTPBodyStream: [NSInputStream inputStreamWithData: [[NSString stringWithFormat:@"username=%@&password=%@&score=%i",[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)ogl_view->username, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)text_field.text, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],ogl_view->score[0]] dataUsingEncoding:NSUnicodeStringEncoding]]];
        NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
        if (connection){
            received_data = [[NSMutableData data] retain];
            ogl_view->section = CURLING_PROCESS_CREDENTIALS;
        }else{
            ogl_view->section = CURLING_LOGIN_OR_REGISTER;
            UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Can't connect to server" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [connection_alert show];
            [connection_alert release];
        }

当我尝试使用网络浏览器时,我可以验证服务器正常运行。

有人可以让我走上正确的轨道,正确地使用 HTTP 和 POST 方法发送数据吗?

感谢您的帮助。

最佳答案

尝试使用它(使用您自己的值):

    NSString* post = @"username=myUser&password=myPass&score=20"; //customize this
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSURL *url = [NSURL URLWithString:@"http://my.request.com"]; //customize this
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData]; 
    [request setTimeoutInterval:timeout];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

然后您需要实现 NSURLConnectionDelegate 方法:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)remoteData;
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

最后记得释放NSURLConnection。

编辑:没看到你写的 setHTTPBody 泄漏......从未见过这种情况发生。不管怎样,这里有一些应该可以工作的代码……

关于iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632475/

相关文章:

iphone - 在iOS中创建声音

objective-c - 类似于 Xcode 4 中的渐变按钮栏

objective-c - 将元胞自动机状态简化为一组向量路径

ios - 如何在上传到用户 Dropbox 之前加密 .plist?

ios - 如何使用 segue 为 Facebook iOS Friend Picker 自定义背景和单元格?

ios - 在 iPhone 上拍照并在 Swift 中保存之前永久覆盖文本

javascript - 按钮在 iPad 中不起作用

ios - UITableView 部分周围的边框

iphone - iOS UITableView 随机崩溃

ios - 如何通过手机APP购买实体商品?付款必须使用 CREDITS 而不是现金