代码之家  ›  专栏  ›  技术社区  ›  Matthew Mitchell

“操作无法完成。通过对等机重置连接。“在iOS上使用nsurlconnection

  •  4
  • Matthew Mitchell  · 技术社区  · 16 年前

    我正在努力将Post数据发送到服务器并接收正确的响应。我开始使用sethttpbody,但当我没有得到某些post数据的正确服务器响应时,我转到了sethttpbodystream,并且我读到sethttpbody在早期的iOS版本上有内存泄漏。问题是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];
            }
    

    我可以验证服务器是否正常,当我尝试使用Web浏览器时,它是否正常工作。

    有人能让我正确地使用http和post方法发送数据吗?

    谢谢你的帮助。

    1 回复  |  直到 15 年前
        1
  •  0
  •   João Caxaria    15 年前

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

        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泄漏…从未见过这种情况。不管怎样,这里有一些代码应该可以工作…