Troubleshooting FORM POST problems
- by brettr
I'm using the following code to submit to a WCF service. I keep getting an error that the key is invalid. I have a webpage on the same server that I submit the same data (but different key) using a FORM POST. It works fine that way. I put the URL below all in a URL (including valid key from server webpage) and get the key is invalid error. I'm thinking the data I'm submitting through the iPhone isn't really going across as a FORM POST but rather as a URL.
Is there anything I may be doing wrong in the following code or any other suggestions?
NSString *stringForURL = @"https://abc.com/someservice.aspx";
NSURL *URL=[[NSURL alloc] initWithString:stringForURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSString *request_body = [NSString stringWithFormat:@"prop1=value1&key=%@",
[@"r2xHEuzgDTQEWA5Xe6+k9BSVrgsMX2mWQBW/39nqT4s=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [request_body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[NSThread sleepForTimeInterval:1];
self.receivedData = [[NSMutableData data] retain];