Q:

"Operation couldn't be completed. Connection reset by peer." with NSURLConnection on iOS

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".
Here's 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 verify the server is fine and it works when I try a web-browser.
Can somebody get me on the right track to sending data with HTTP and the POST method correctly?
Thank you for any help.

A:

Try using this (with your own values):
    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];

You'll then need to implement the NSURLConnectionDelegate methods:
    - (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;

In the end, remember to release the NSURLConnection.
Edit: Didn't see that you wrote that setHTTPBody leaks... never seen that happening. Anyway, here is some code that should work...

