Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top