当数据包含&符号时,从iOS应用程序调用asp.net Web服务崩溃?

时间:2012-06-23 08:16:01

标签: ios web-services nsmutableurlrequest

我有一个实际调用asp.net Web服务的应用程序但是当我用&传输数据时它停止工作。如下所示jsonString有first_name有“Pranav&”所以当这个阶段我调用服务并在asciistringencoding中转换它时会因为ascii转换它而产生错误

&quotPranav &&quot

发现两个&符号后停止工作

NSString * jsonString = @"{"home_phone":"123","GroupId":"GP00000099","first_name":"Pranav &","city":"df","last_name":"Purohit","state":"sl","email":"pjp@gmail.com","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];

1 个答案:

答案 0 :(得分:0)

我得到了答案,当您嵌入& 将其替换为 \ u0026