将 NSURL 与 PHP Web 服务结合使用,该服务在其 get 请求中使用括号
我正在编写一个 iPhone 应用程序,需要从 Web 服务获取数据。在此服务中,同一个变量有可能具有多个值,并且使用 [ ] 括号访问该变量。 (我不是 PHP 开发人员,但我知道这种方案对于 PHP 服务来说并不罕见。)
因此示例请求可能如下所示:
example.com/service.php?id[]=1&id[]=2
我用来形成 URL 的代码如下所示:
NSString *string = @"http://example.com/service.php?id[]=1&id[]=2";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *networkRequest = [NSMutableURLRequest requestWithURL:url];
...
问题是,在第二行之后,URL 会自动编码,因此URL确实尝试访问如下所示:
example.com/service.php?id%5B%5D=1&id%5B%5D=2
结果我的请求没有访问到我想要的信息。有没有办法阻止 NSURL 对我的 URL 进行编码,以便它可以访问此 Web 服务?
I am writing an iPhone application that needs to get data from a web service. In this service, there is a possibility of the same 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我而言,事实证明,与服务所有者的通信揭示了导致问题的服务器上的不规则行为,但我将其归因于 NSURL 对字符串的处理。我深表歉意,并感谢您的回复。
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.
我在我的应用程序中遇到了这样的问题,我通过在构造 url 之前对 url 字符串进行编码来解决。所以我认为你需要这样做
请参考链接更多如果没有。
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
Please refer the link for more ifno.
也许只是简单地逃避它们:
它可能最终也会对反斜杠进行 URLEncoding,但值得一试。
Perhaps simply escape them:
It's possible that it will just end up URLEncoding the backslashes, too, but it's worth a shot.