How do you programmatically find a true URL in C # instead of a forwarding link?

Another way to ask this question is: how do you programmatically extend the TinyURL link to its true location?

What I want to do is find a way to programmatically take the link:

http://www.baidu.com/link?url=mW91GJqjJ4zBBpC8yDF8xDhiqDSn1JZjFWsHhEoSNd85PkV8Xil7qccoOX3rynaE 

(the first link in Jessica Alba's search using baidu.com) and return her actual link:

 http://baike.baidu.com/view/270790.htm 

This is just one example. What I want to do is not specific to Jessica, but to use Baidu.com as part of my search engine group in my meta-search project.

There may be a way to use the WebBrowser class, but I have not seen the member that was the URL.

There may be a way to use WebRequest and WebResponse .

+6
source share
3 answers

Request a tinied URL and parse the HTTP Location response header.

+2
source

Here you go, it's nice and easy!

 var WReq = WebRequest.Create("http://www.baidu.com/link?url=mW91GJqjJ4zBBpC8yDF8xDhiqDSn1JZjFWsHhEoSNd85PkV8Xil7qccoOX3rynaE"); WReq.Method = "HEAD"; // Only download the headers, not the page content var ActualURL = WReq.GetResponse().ResponseUri.ToString(); MessageBox.Show(ActualURL); 

+2
source
 string url = "http://www.baidu.com/link?url=mW91GJqjJ4zBBpC8yDF8xDhiqDSn1JZjFWsHhEoSNd85PkV8Xil7qccoOX3rynaE"; var req = (HttpWebRequest)HttpWebRequest.Create(url); req.AllowAutoRedirect = false; //<--!!!! var resp = req.GetResponse(); var realUrl = resp.Headers["Location"]; //http://baike.baidu.com/view/270790.htm 

PS: The key here is req.AllowAutoRedirect = false

+1
source

All Articles