如何在请求中使用 get 参数处理重定向 url? [英] How to handle redirecting urls with get parameters in the request?

查看:49
本文介绍了如何在请求中使用 get 参数处理重定向 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器 (aspx) 获取 html 响应.

I am trying to get an html response from a server(aspx).

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://www.someSiteThatRedirectsWithGetParameters.com?parameter1=1&parameter2=2"];

NSURL *url = [ NSURL URLWithString:urlstr];     
NSMutableURLRequest *urlRequest = [ NSURLRequest requestWithURL: url];

NSData data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

NSURL *lastURL=[response URL];

NSLog(@"Last url %@", lastURL);

我得到的 lastURL 是 www.someSiteThatRedirectsWithGetParameters.com,因此没有得到原始请求 url 的 html 页面.

I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com and therefore not getting the html page for the original requested url.

为什么会这样,我该如何解决?

Why is this so and how can I solve this?

编辑

这就是我到目前为止所尝试的所有内容:获取最后一个iOS 5 中的重定向网址?但我仍然无法实现我想要的目标,即:获取原始 GET 请求(通过重定向)的 html 页面源,在 url 中有参数?

This is all what I have tried so far : Get the last redirected url in iOS 5? but I am still not able to achieve what I am trying to i.e : get the html page source of the original GET request(going through redirections) having parameters in the url ?

推荐答案

我得到的 lastURL 是 www.someSiteThatRedirectsWithGetParameters.com因此没有获得原始请求的 html 页面网址.

I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com and therefore not getting the html page for the original requested url.

如果您的请求被重定向,那是因为您连接的服务器告诉您,您第一次向其发出请求的 URL 中没有页面,您应该使用重定向的 URL.这就是重定向响应的全部意义所在.

If your request is being redirected, it's because the server to which you're connecting is telling you that there is no page at the URL to which you first made the request, and you should use the redirected URL instead. That's the whole point of a redirect response.

如果您得到的响应是 HTTP 303,新请求将正确更改从 POST 到 GET.有时这样做是为了避免刷新页面会重新提交原始请求并带来不良后果的情况.在这种情况下,您将请求 POST 到某个 URL,服务器接受该请求,但通过将客户端重定向到最终显示服务器响应的不同 URL 进行响应.

If the response that you get is a HTTP 303, the new request will properly be changed from POST to GET. This is sometimes done to avoid cases where refreshing the page would resubmit the original request with undesirable consequences. In this case, you POST your request to some URL, and the server accepts the request but responds by redirecting the client to a different URL which ultimately displays the server's response.

为什么会这样,我该如何解决?

Why is this so and how can I solve this?

我已经解释了上面的为什么".解决方案"是接受来自服务器的响应并按照重定向到包含服务器响应的 URL.

I've explained the 'why' above. The "solution" is to accept the response from the server and follow the redirect to the URL that contains the server's response.

这篇关于如何在请求中使用 get 参数处理重定向 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆