Reputation: 53
I am trying to send the data to server from my IPhone client. It works fine for most values but when i try to send a string like "IPhone+Cocoa" the server shows the string as "IPhone Cocoa". I have tried to google it but without success is there any why of doing it.
Here is my code
-(void)sendRequestNSString *)aRequest {
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kURLRequest]];
NSString *httpBody =
[NSString stringWithFormat:@"%@=%@",
[requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding],
[aRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding]];
NSData *aData = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:aData];
[request setHTTPMethod:@"POST"];
self.feedURLConnection =
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
I am really having trouble finding the solution for this. Is this a bug or I am wrong some where in my code. is there ant other alternative of doing this
Upvotes: 1
Views: 751
Reputation: 8408
-[NSString stringByAddingPercentEscapesUsingEncoding:]
shouldn't be used for encoding parameter values. It leaves certain special characters (+, &, etc) unencoded (as they should be if you were encoding a URL).
Use the following:
NSString *escapedParameter = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)unescapedParameter, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];
Not as pretty as stringByAddingPercentEscapesUsingEncoding:
, but you could easily make a macro for this.
http://arstechnica.com/apple/news/2009/01/iphone-dev-building-proper-mailto-urls.ars was written up back in January when I discovered similar problems with building mailto: URLs to send to -[UIApplication openURL:]
.
N.B. Welcome to Stack Overflow! Don't forget to read the FAQ and mark accepted answers for your questions (if they're good answers worth accepting, of course).
Upvotes: 4