Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-22-2008, 10:55 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 160
Vortec4800 is on a distinguished road
Default 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?
Vortec4800 is offline   Reply With Quote
Old 12-22-2008, 10:59 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 160
Vortec4800 is on a distinguished road
Default

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>
Vortec4800 is offline   Reply With Quote
Old 01-15-2009, 04:59 PM   #3 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 3
oxrxb is on a distinguished road
Default

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.
oxrxb is offline   Reply With Quote
Old 01-17-2009, 01:29 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
ethanwa is on a distinguished road
Default

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?
ethanwa is offline   Reply With Quote
Old 01-17-2009, 01:46 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Seattle, WA USA
Posts: 590
ethanwa is on a distinguished road
Default

Did you guys read this? It just solved my problem:

NSURLConnection POST to send & receive data. - iDevKit
ethanwa is offline   Reply With Quote
Old 10-27-2009, 04:35 AM   #6 (permalink)
krishnan
 
krish's Avatar
 
Join Date: Sep 2009
Location: Chennai
Posts: 38
krish is on a distinguished road
Send a message via Yahoo to krish
Default Lost network Conection

Quote:
Originally Posted by ethanwa View Post
Did you guys read this? It just solved my problem:

NSURLConnection POST to send & receive data. - iDevKit

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.
krish is offline   Reply With Quote
Old 09-16-2010, 03:21 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 22
mouton is on a distinguished road
Default

I have the same issue, please help..

Quote:
Originally Posted by krish View Post
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.
mouton is offline   Reply With Quote
Old 09-16-2010, 03:40 AM   #8 (permalink)
krishnan
 
krish's Avatar
 
Join Date: Sep 2009
Location: Chennai
Posts: 38
krish is on a distinguished road
Send a message via Yahoo to krish
Default

Quote:
Originally Posted by mouton View Post
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.
__________________
krishnan
Blog Address
krish is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 339
6 members and 333 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0