【问题标题】:"Operation couldn't be completed. Connection reset by peer." with NSURLConnection on iOS“操作无法完成。对等方重置连接。”在 iOS 上使用 NSURLConnection
【发布时间】:2010-09-03 01:34:17
【问题描述】:

我正在努力将 POST 数据发送到服务器并接收正确的响应。我从使用 setHTTPBody 开始,但是当我没有获得某些 POST 数据的正确服务器响应并且我读到 setHTTPBody 在早期 iOS 版本上存在内存泄漏时,我转向了 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 方法发送数据吗?

感谢您的帮助。

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

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

        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 泄漏......从来没有见过这种情况发生。无论如何,这里有一些应该可以工作的代码......

    【讨论】:

    • github 中有一个示例项目,其中包含我用来执行请求的几个类。希望对您有所帮助。
    猜你喜欢
    • 2013-09-22
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2015-10-20
    • 2018-02-10
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多