Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Operation couldn't be completed. Connection reset by peer." with NSURLConnection on iOS
    primarykey
    data
    text
    <p>I'm struggling sending POST data to a server and receiving the correct response. I started by using setHTTPBody but moved to setHTTPBodyStream when I wasn't getting the correct server response for certain POST data and I read setHTTPBody has a memory leak on earlier iOS versions. The problem is setHTTPBodyStream causes the error - "Operation couldn't be completed. Connection reset by peer".</p> <p>Here's the code:</p> <pre><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=%@&amp;password=%@&amp;score=%i",[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)ogl_view-&gt;username, NULL, CFSTR("?=&amp;+"), kCFStringEncodingUTF8) autorelease],[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)text_field.text, NULL, CFSTR("?=&amp;+"), kCFStringEncodingUTF8) autorelease],ogl_view-&gt;score[0]] dataUsingEncoding:NSUnicodeStringEncoding]]]; NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self]; if (connection){ received_data = [[NSMutableData data] retain]; ogl_view-&gt;section = CURLING_PROCESS_CREDENTIALS; }else{ ogl_view-&gt;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]; } </code></pre> <p>I can verify the server is fine and it works when I try a web-browser.</p> <p>Can somebody get me on the right track to sending data with HTTP and the POST method correctly?</p> <p>Thank you for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload