実際にasp.netWebサービスを呼び出すアプリケーションがありますが、それを使用してデータを転送すると機能し&
なくなります。以下のように、jsonStringには「Pranav&」を持つfirst_nameがあるため、この段階でasciistringencodingで変換してサービスを呼び出すと、asciiが次のように変換するためエラーが発生します。
"Pranav &"
2つのアンパサンドを見つけると、機能しなくなります
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];