开发者

Detecting 302 Redirect

开发者 https://www.devze.com 2023-03-15 23:12 出处:网络
I\'m trying to check the redirect location of a url but am always getting the wrong results. For example, for the url http://www.yellowpages.com.eg/Mjg3NF9VUkxfMTEwX2h0dHA6Ly93d3cubG90dXMtYWlyLmNvbV8=

I'm trying to check the redirect location of a url but am always getting the wrong results. For example, for the url http://www.yellowpages.com.eg/Mjg3NF9VUkxfMTEwX2h0dHA6Ly93d3cubG90dXMtYWlyLmNvbV8=/Lotus-Air/profile.html, it redirects to http://www.lotus-air.com with a type of redirect 302 Found (you can test it on the this service http://www.internetofficer.com/seo-tool/redirect-check/), however am getting "http://mobile.yellowpages.com.eg/" as the webResp.GetResponseHeader("Location") . My Code is as follows:

        string url = @"http://www.yellowpages.com.eg/Mjg3NF9VUkxfMTEwX2h0dHA6Ly93d3cubG90dXMtYWlyLmNvbV8=/Lotus-Air/profile.html";


        HttpWebRequest webReq = WebRequest.Create(url) as HttpWebRequest;
        webReq.Method = "HEAD";
        webReq.AllowAutoRedirect = false;
        HttpWe开发者_Python百科bResponse webResp = webReq.GetResponse() as HttpWebResponse;
        txtOutput.Text += webResp.StatusCode.ToString() + "\r\n" ;
        txtOutput.Text += webResp.GetResponseHeader("Location") + "\r\n";
        txtOutput.Text += webResp.ResponseUri.ToString();

        webResp.Close();

Thanks.

Yehia


They are probably sending different redirects based on the user agent, so you get one result in a browser and another in your code.


You could use a HTTP debugging proxy to get an understanding of the headers moving back and forth and enables to you to change your user-agent to help test Ben's theory (I +1'd that).

A good one is Fiddler - Web Debugging Proxy free and easy to use/

The screenshot below shows me changing the useragent to an old IEMobile one "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12; en-US; KIN.Two 1.0)", which redirects me to mobile.yellowpages.com.eg

n.b. changing to an ipad useragent takes you to iphone.yellowpages.com.eg

Detecting 302 Redirect


As Ben pointed out, it redirects based on user agent. Just add some user agent (this one is for chrome):

webReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";

For me it redirects to http://www.lotus-air.com.

0

精彩评论

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