If I have a URL like http://popurls.com/go/msn.com/l4eba1e6a0ffbd9fc915948434423a7d5, how do I expand it back to the original URL programmatically? O开发者_开发知识库bviously I could use an API like expandurl.com but that will limits me to 100 requests per hour.
Make a request to the URL and check the status code returned. If 301
or 302
, look for a Location
header, which will contain the "expanded URL":
string url = "http://popurls.com/go/msn.com/l4eba1e6a0ffbd9fc915948434423a7d5";
var request = (HttpWebRequest) WebRequest.Create(url);
request.AllowAutoRedirect = false;
var response = (HttpWebResponse) webRequest.GetResponse();
if ((int) response.StatusCode == 301 || (int) response.StatusCode == 302)
{
url = response.Headers["Location"];
}
Note: This solution presumes that only one redirect occurs. This may or may not be want you need. If you simply want to deobfuscate URLs from obfuscators (bit.ly et al) this solution should work well.
Managed to find an answer.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://popurls.com/go/msn.com/l4eba1e6a0ffbd9fc915948434423a7d5");
req.AllowAutoRedirect = true;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
ServicePoint sp = req.ServicePoint;
Console.WriteLine("End address is " + sp.Address.ToString());
精彩评论