Search code examples
iosweb-servicesnsmutableurlrequest

Calling asp.net web service from iOS application crash when data contains ampersands?


I have an application which actually calling asp.net web service but when I transfer data with & it stop working. Like below jsonString have first_name which have "Pranav &" so as this stage when I call service with converting it in asciistringencoding it generate error because of ascii convert it like

&quotPranav &&quot

with finding two ampersands it stop working

NSString * jsonString = @"{"home_phone":"123","GroupId":"GP00000099","first_name":"Pranav &","city":"df","last_name":"Purohit","state":"sl","email":"[email protected]","member_Id":"GM00008513"}";
NSString * post = [[NSString alloc] initWithFormat:@"jsonText=%@", jsonString];
NSLog(@"%@",post);
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.myweburl.com/mywebservice.asmx/mymethod"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Solution

  • I got answer, when you embed & replace it with \u0026