开发者

HttpWebRequest.Address vs HttpWebResponse.ResponseUri

开发者 https://www.devze.com 2022-12-12 07:44 出处:网络
Whats the difference between these two properties? To put into context, I am determining if a redirect occurs if our ResponseUri != RequestUri.

Whats the difference between these two properties?

To put into context, I am determining if a redirect occurs if our ResponseUri != RequestUri.

While a redirect occurs regardless the url http://adage.com/adages/article?article_id=140560 will provide a different ResponseUri (http://adage.com/adages/post.php) than the Address (http://adage.com/adages/post?article_id=140560).

It appears the ResponseUri takes the Content-Location header and uses it while the Address main开发者_运维技巧tains the correct location.

Would it be correct to compare the RequestUri to the HttpWebRequest.Address to check for redirects?


Yes, comparing request.RequestUri and request.Address is the way to go. At least in Mono response.ResponseUri is the same as request.Address.


I know this is an old question, but I found it while researching this topic and noticed it wasn't actually answered correctly.

While HttpWebRequest.Address and HttpWebResponse.ResponseUri should always be the same, here is the difference:

  • HttpWebResponse.Address will return the Uri of the page actually responding
  • HttpWebResponse.ResponseUri will return the value of the Content-Location header (if present). While the documentation doesn't explicitly state what happens if the Content-Location header is not present, it is assumed it will use the same value as Address.

Remember HTTP headers can be forged, so Microsoft recommends using Address instead of ResponseUri for security reasons.

http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.responseuri.aspx


Have you thought about setting request.AllowAutoRedirect=false and then reissuing the request on a redirect?

The Uri comparison should also work fine, although I am not sure of all the edge cases

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号