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 01-04-2011, 09:05 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
kunalpatel is on a distinguished road
Default Help with Simple HTTP POST using NSURLConnection

Hello,

I'm literally a newbie to iPhone Development. Amongst all the other wonders, I'm trying to POST username & password to an 'already LIVE' webpage which, upon correct credentials will return a Welcome message for me. The bit I'm left wondering about is that there's no call to NSURLConnection delegate method didReceiveData. I've checked (using NSLog) other delegate methods like didReceiveResponse, connectionDidFinishLoading and they are being called also there's no call being made to didFailWithError, which makes me assume that there's no error occurring in the process. Following is my code, I'd be grateful if anyone could point me in the right direction.

Code:
- (IBAction)UpdateLabel:(id)sender
{
	BOOL flag = YES;
	self.username = textField.text;
	self.password = passField.text;
	
	NSString *uname = self.username;
	NSString *pass  = self.password;
	
	if([uname length] == 0)
	{
		uname = @"Enter something for Christmas's sake!";
		flag = NO;
	}
	if ([pass length] == 0)
	{
		pass = @"Where's the password mate ?!";
		flag = NO;
	}
	
	if(flag)
	{
		NSString *post			= [[NSString alloc] initWithFormat:@"Username=%@&Password=%@", uname, pass];
		NSMutableData *postData	= [NSMutableData data];
		[postData appendData:[post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
		NSString *postLength	= [NSString stringWithFormat:@"%d", [postData length]];

		NSURL    *url = [NSURL URLWithString:@"http://www.2sms.com/iPhoneApp.aspx"];		
		NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
		[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];
		
		NSString *displayURL = [[NSString alloc] initWithFormat:@"%@%@", url, post];
		
		label.text = displayURL;
		
		finished = NO;
		
//I have also tried synchronous request, only to no avail.
		/* 
		NSError *error;
		NSHTTPURLResponse *response;
		NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
		*/
				
		connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
		if (connection)
		{
			while (!finished)
			{
				[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
			}
			receivedData = [[NSMutableData data] retain];
		}
		
					
		@try 
		{
			//NSString *returnData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
			label.text = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
			NSLog(@"%@", [label text]);
		}
		@catch (NSException *e)
		{
			
		}
		@finally 
		{
			[post release];
			[displayURL release];
		}
	}
}
...and NSURLConnection Delegate Methods
Code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	NSLog(@"From ReceivedResponse");
	[self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	NSLog(@"From ReceiveData");
	if(self.receivedData == nil)
	{
		self.receivedData = [[NSMutableData alloc] initWithLength:2048];
	}
	[self.receivedData appendData:data];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{	
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSLog(@"%d bytes of data", [receivedData length]);
	finished = YES;
	[receivedData release];
	[connection release];
}
kunalpatel is offline   Reply With Quote
Old 01-05-2011, 04:07 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
kunalpatel is on a distinguished road
Default

Can anyone please help ? The matter has got bit urgent of nature at my work place.

Thanks,
Kunal
kunalpatel is offline   Reply With Quote
Old 01-05-2011, 05:08 AM   #3 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

relly i not use nsurlconnection, so my answear is:
-create a sample project (so i can see what happen)
-use ASIHTTPRequest (i love it, simple and powerfull)

ASIHTTPRequest example code - All-Seeing Interactive
__________________
dany_dev is offline   Reply With Quote
Old 01-05-2011, 10:53 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
kunalpatel is on a distinguished road
Default

Quote:
Originally Posted by dany88 View Post
relly i not use nsurlconnection, so my answear is:
-create a sample project (so i can see what happen)
-use ASIHTTPRequest (i love it, simple and powerfull)

ASIHTTPRequest example code - All-Seeing Interactive
Thanks Dany, for your reply and suggestion.

Sadly, I've tried using ASIHTTPRequest and it was all in vain Though, when I sent a request to a different, .jsp URL it worked, I don't know why, but it just did ! The previous request was to a .aspx page, in case anyone wonders.
kunalpatel is offline   Reply With Quote
Reply

Bookmarks

Tags
didreceivedata, http, nsurlconnection, post

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: 362
7 members and 355 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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