有没有一种方法可以确定在WebRequest期间是否发生了重定向? [英] Is there a way to determine if a redirect occurrs during a WebRequest?
问题描述
我正在使用HttpWebRequest刮取Wikipedia.org.很多时候,页面上的主题链接会被合并,因此它们会将您重定向到正确的页面.
I'm using HttpWebRequest to scrape Wikipedia.org. A lot of times there will be links to topics on a page that have been consolidated and therefore they redirect you to the correct page.
例如
http://en.wikipedia.org/wiki/Polish_prisoners_of_war_in_Soviet_Union_(after_1939)
将您重定向到正确的主题
redirects you to the correct topic which is
http://en.wikipedia.org/wiki/Polish_prisoners_of_war_in_the_Soviet_Union_(after_1939) a>
http://en.wikipedia.org/wiki/Polish_prisoners_of_war_in_the_Soviet_Union_(after_1939)
请注意添加了"the"一词.
Notice the addition of the word "the".
这时我需要确定是否发生了重定向.谁能建议我该怎么做?
I need to determine at this point whether or not a redirect has happened. Can anyone suggest how I might do this?
谢谢!
更新
我在下面将回答标记为已回答,因为从技术上讲,这是告诉您是否已重定向的方式.我遇到的问题是Wikipedia实际上没有使用http响应代码3xx进行硬重定向.他们正在执行软重定向,该服务会在同一网址下提供不同的内容.我将不得不找到另一种解决方案.
I marked the response below as answered because technically that is how you tell if you have been redirected. The problem I am having is the Wikipedia is not actually doing a hard redirect with http response codes 3xx. They are doing soft redirects which serves up different content under the same Url. I'll have to find another solution.
推荐答案
尝试一下:
if(reponse.ResponseUri != request.RequestUri) {
//You were redirected
}
这篇关于有没有一种方法可以确定在WebRequest期间是否发生了重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!