12-22-2008, 10:55 PM
#1 (permalink )
Registered Member
Join Date: Sep 2008
Posts: 160
NSURLConnection POST debugging, lost network connection
I'm doing what I would think is a simple POST to a server on my local network. This works in standard HTML using a form with two fields, a hidden field with an ID key and a few different submit buttons (multiple buttons all with name="submit" and different values to determine the action. When I try to replicate this POST message in code it does not work, my NSURLConnection returns with an error reading "lost network connection" and I'm not exactly sure why, or how to debug this. Here is the code that generates my POST message:
Code:
NSString *URLString;
NSStringEncoding encoding = NSUTF8StringEncoding;
NSString *url = [NSString stringWithFormat:@"http://admin:admin@192.168.1.110/edit"];
URLString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)url, NULL, NULL, encoding);
NSLog(@"%@", URLString);
// Create request and set it up
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: URLString]];
[request setHTTPMethod:@"POST"];
NSMutableData *requestBodyData = [NSMutableData data];
// Create boundry
NSString *boundary = [NSString stringWithFormat:@"--%@--", [[NSProcessInfo processInfo] globallyUniqueString]];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSData *boundaryData = [[[[@"--" stringByAppendingString:boundary] stringByAppendingString:@"\r\n"] dataUsingEncoding:encoding] retain];
// Add ID to POST
[requestBodyData appendData:boundaryData];
[requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; "] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", @"node"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"C 9D 67 1"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:boundaryData];
// Submit
[requestBodyData appendData:boundaryData];
[requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; "] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", @"submit"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"Go"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:boundaryData];
// Send out request
[request setHTTPBody:requestBodyData];
NSURLResponse *response = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(response){
NSLog(@"Response: %@", response);
}
else{
NSLog(@"Error: %@", [error localizedDescription]);
}
Is there something wrong with my POST message? How can I find out why the connection is being dropped?
12-22-2008, 10:59 PM
#2 (permalink )
Registered Member
Join Date: Sep 2008
Posts: 160
Here is the HTTP form equivalent that I am shooting for:
Code:
<form method="POST" action="/edit">
<input type="hidden" name="node" value="C 9D 67 1"/>
<input type="submit" name="submit" value="Go"/>
<input type="submit" name="submit" value="Stop"/></form>
01-15-2009, 04:59 PM
#3 (permalink )
New Member
Join Date: Jan 2009
Posts: 3
I have an HTTP form that is very similar to Vortec's. My connection was successful but unfortunately the returned response was not correct. Perhaps I am not creating the needed data correctly?
Here's my code:
Code:
NSString *ZFRversion = [NSString stringWithString:@"3"];
NSString *ZFRtype = [NSString stringWithString:@"ZFRMsg"];
NSString *ZFRdata = [NSString stringWithString:@"Af42EkUEsDADnboLw0gAAANIgYBNRjR3RDah5IFxmL9vT3k7CA"];
NSString *ZFRsubmit = [NSString stringWithString:@"Read Message"];
NSString *post = [NSString stringWithFormat:@"ZFRversion=%@&ZFRtype=%@&ZFRdata=%@&ZFRsubmit=%@, ZFRversion, ZFRtype, ZFRdata, ZFRsubmit];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postData];
//where request is an instance of NSMutableURLRequest
Any help is very much appreciated. Thanks in advance.
01-17-2009, 01:29 AM
#4 (permalink )
Registered Member
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
I too would like an answer to this. How do you do a simple basic POST to a server, similar to the <form> posted above?
01-17-2009, 01:46 AM
#5 (permalink )
Registered Member
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
10-27-2009, 04:35 AM
#6 (permalink )
krishnan
Join Date: Sep 2009
Location: Chennai
Posts: 38
Lost network Conection
Quote:
Originally Posted by
ethanwa
Hi ,
I was trying to access the URL specified. But I could not.
My Problem:
I use NSURLConnection to access https server. but I always get "lost network connection". Could you help me out of this trouble?
Regards,
krishnan.
09-16-2010, 03:21 AM
#7 (permalink )
Registered Member
Join Date: Aug 2009
Posts: 22
I have the same issue, please help..
Quote:
Originally Posted by
krish
Hi ,
I was trying to access the URL specified. But I could not.
My Problem:
I use NSURLConnection to access https server. but I always get "lost network connection". Could you help me out of this trouble?
Regards,
krishnan.
09-16-2010, 03:40 AM
#8 (permalink )
krishnan
Join Date: Sep 2009
Location: Chennai
Posts: 38
Quote:
Originally Posted by
mouton
I have the same issue, please help..
Hello,
If you are testing in Simulator, start testing in the Device. I had that problem in Simulator but not in my iPhone.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44