Yehia A.Salam
Yehia A.Salam

Reputation: 2078

Detecting 302 Redirect

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;
        HttpWebResponse 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

Upvotes: 0

Views: 1216

Answers (3)

Alex KeySmith
Alex KeySmith

Reputation: 17101

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

Switching the user agent to one of a mobile phone, redirects you to mobile.yellowpages.com.eg

Upvotes: 2

mlusiak
mlusiak

Reputation: 1054

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.

Upvotes: 1

Coding Flow
Coding Flow

Reputation: 21881

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

Upvotes: 3

Related Questions