"The operation cannot be completed. The connection is reset using a peer-to-peer network." with NSURLConnection on iOS

I struggle by sending POST data to the server and getting the correct answer. I started by using setHTTPBody, but switched to setHTTPBodyStream when I did not get the correct server response for certain POST data, and I read that setHTTPBody has a memory leak on earlier versions of iOS. The setHTTPBodyStream problem causes an error - "Operation could not be completed. Connection reset on peers."

Here is the code:

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];
        }

I can check that the server is ok and it works when I try to use a web browser.

Can someone get me on the right track to send data correctly using HTTP and the POST method?

Thanks for any help.

+3
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 ... , . , , ...

0

Source: https://habr.com/ru/post/1762992/


All Articles