NSURLConnection POST issues
NSURLConnection POST issues
- Subject: NSURLConnection POST issues
- From: Damien Cooke <email@hidden>
- Date: Sun, 10 Jan 2010 14:01:13 +1030
Hi All,
I have a solution for this problem that I am not at all happy with. I have appended all of my html form variables into a single variable using ||| as the field separators. This is a rubbish solution. I am looking for a more elegant solution that is less like a dodgy hack.
Here is the problem and I am sure there is a reasonable and simple explanation for my problem that I have over looked but I can not see it yet.
The Java servlet I am connecting to expects 6 variables:
String strProdName = request.getParameter("ProdName");
String strProdVersion = request.getParameter("ProdVersion");
String strProdBuild = request.getParameter("ProdBuild");
String strUserEmail = request.getParameter("UserEmail");
String strUserComment = request.getParameter("UserComment");
String strUserDevice = request.getParameter("UserDevice");
here is my code to send this data
- (void) transmitMessageToServer
{
NSMutableData *appendedData = [[NSMutableData alloc] init];
[appendedData appendData:[[[NSString stringWithFormat:@"ProdName=%@",[self applicationName]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[appendedData appendData:[[[NSString stringWithFormat:@"ProdVersion=%@",[self productVersion]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[appendedData appendData:[[[NSString stringWithFormat:@"ProdBuild=%@",[self productBuild]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[appendedData appendData:[[[NSString stringWithFormat:@"UserEmail=%@",[self userEmail]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[appendedData appendData:[[[NSString stringWithFormat:@"UserComment=%@",[self userComment]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[appendedData appendData:[[[NSString stringWithFormat:@"UserDevice=%@",[self userDevice]] stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
NSString *dataLength = [[NSString alloc] initWithFormat:@"%d", [appendedData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://somesite.com:8080/addcomment"]];
[request setHTTPMethod:@"POST"];
[request setValue:dataLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:appendedData];
[appendedData release];
[postLength release];
[request release];
NSURLConnection *messageConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if(messageConnection)
{
responseData = [[NSMutableData data] retain];
}else
{
NSLog(@"No network connection available?");
}
}
When I send this all of the data appears as a single string attached to html variable named ProdName. I suspect this is because all of the data is appended together. I have tried many variations including dictionaries etc the problem appears to be that body is NSData and not an Array or collection of some type which would have made more sense to me. (please correct me if I am wrong in my thinking) So how do I send multiple pieces of data using POST and have a non Cocoa aware application separate it at the other end? If some one could assist me understand how this is supposed to work I would be grateful. Or is NSURLConnection and NSMutableRequest the wrong set of classes for this purpose?
Regards
Damien
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden