Httpwebrequest / Httpwebresponse - Redirect Count

孤街醉人 提交于 2019-12-10 20:14:31

问题


I'm trying to figure out how many times my web request was redirected before I finally landed at the end content.

I'm creating my web request as follows:

var httpRequest = (HttpWebRequest) WebRequest.Create("some arb path");
httpRequest.AllowAutoRedirect = followRedirects; 

I've had a look at the following URL http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.maximumautomaticredirections.aspx

However, I do not want to impose the limit. I actually want it to follow all redirects (not really wanting a reference to all the URL's), but just for example saying "You have been redirected X times".

I hope there's a quick way to do it as currently I'm assuming I would have to capture all 3xx codes and create a new request for each (hopefully not!).


回答1:


There is no way to achieve what you want. You will have to capture each 3xx request and issue a new one with the location header of the redirect.

However, if you want to use native C/C++ code and write directly to WININET (which is the library used by IE) it will provide you (via a callback mechanism) notification for each redirect that happened.



来源:https://stackoverflow.com/questions/3404493/httpwebrequest-httpwebresponse-redirect-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!