문제

I have encountered an issue with my code, when I want to send an NSString variable to my WCF web service.

Code:

NSString *str= @"http://IPdAddress/myService.svc/json/";
str=[str stringByAppendingFormat:@"InsertNewWeight/%@",noteWeight.text];

NSURL *WcfSeviceURL = [NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:WcfSeviceURL];

[request setHTTPMethod:@"POST"];

This code is working fine if I enter the noteWeight.text value as a simple word.

When I entered the noteWeight.text value as a complex word (two words separated by a space) the system returns an error data parameter is nil

Can anybody help me please on this? Thank you.

도움이 되었습니까?

해결책

As it is passed in the URL and not as a POST argument, you should URL-encode your noteWeight.text string, like this:

str = [str stringByAppendingFormat:@"InsertNewWeight/%@", [noteWeight.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

The spaces will be sent as %20, and other special characters will also be encoded to be correctly read on the server-side.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top