I am writing an iPhone application that needs to get data from a web service. In this service, there is a possibility of the sa开发者_如何学编程me variable having multiple values, and this variable is accessed using [ ] brackets. (I am not a PHP developer, but I understand this scheme is not uncommon for PHP services.)
So a sample request might look like:
example.com/service.php?id[]=1&id[]=2
The code I am using to form the URL looks like this:
NSString *string = @"http://example.com/service.php?id[]=1&id[]=2";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *networkRequest = [NSMutableURLRequest requestWithURL:url];
...
The problem is that, after the second line, the URL is automatically encoded, so that the URL is really tries to access looks like this:
example.com/service.php?id%5B%5D=1&id%5B%5D=2
As a result, my request does not access the information I want. Is there a way to prevent NSURL from encoding my URL so that it can access this web service?
In my case, it turned out that communication with the service owner revealed irregular behavior on the server that caused the problem, but I was attributing it to NSURL's handling of strings. My apologizes, and thank you for your responses.
I was having such issue in my application, I solved by encoding my url string before construction of the url. So I think u need to do it like this
NSString *string = @"http://example.com/service.php?id%5B%5D=1&id%5B%5D=2";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *networkRequest = [NSMutableURLRequest requestWithURL:url];
Please refer the link for more ifno.
Perhaps simply escape them:
NSString *string = @"http://example.com/service.php?id\[\]=1&id\[\]=2";
It's possible that it will just end up URLEncoding the backslashes, too, but it's worth a shot.
精彩评论