user1079052
user1079052

Reputation: 3833

NSMutableURLRequest sending null value on device but works in browser

Before you read this please keep in mind that I am an Objective C programmer trying to help debug a server problem with a C# programmer who knows nothing about what I do.

I am sending a SOAP packet to a .net/c# side and the server receives nulls for the variables. When the url and variable string is put in the browser I get the proper response.

The post logs out to "?help=1& [email protected] & password=testpassword" but i keep getting "Result=Error&Details=Object reference not set to an instance of an object." back for my return string.

NSString *post = [NSString stringWithFormat:@"?help=1& email=? & password=%@",userEmail, userPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:LOGIN_SERVICE]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//Submit the Post:
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

/NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//Extract Return:
NSString *returnString = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];

Upvotes: 1

Views: 606

Answers (1)

WDUK
WDUK

Reputation: 19030

Your email variable within the format string wasnt being populated. And also, why do you have spaces in your post data? I imagine this is causing your issues. Because your data can contain spaces, you'll need to escape them.

Instead of:

NSString *post = [NSString stringWithFormat:@"?help=1& email=? & password=%@",userEmail, userPassword];

Try:

NSString* escapedUserEmail = [userEmail stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* escapedUserPassword = [userPassword stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSString *post = [NSString stringWithFormat:@"?help=1&email=%@&password=%@",escapedUserEmail, escapedUserPassword];

For more reading on the topic, refer to: http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html

Upvotes: 2

Related Questions