将 NSURL 与 PHP Web 服务结合使用,该服务在其 get 请求中使用括号

发布于 2024-10-15 07:03:28 字数 675 浏览 2 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-10-22 07:03:28

就我而言,事实证明,与服务所有者的通信揭示了导致问题的服务器上的不规则行为,但我将其归因于 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.

葬﹪忆之殇 2024-10-22 07:03:28

我在我的应用程序中遇到了这样的问题,我通过在构造 url 之前对 url 字符串进行编码来解决。所以我认为你需要这样做

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];

请参考链接更多如果没有。

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.

樱娆 2024-10-22 07:03:28

也许只是简单地逃避它们:

NSString *string = @"http://example.com/service.php?id\[\]=1&id\[\]=2";

它可能最终也会对反斜杠进行 URLEncoding,但值得一试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文