如果我有一个URL,例如http://popurls.com/go/msn.com/l4eba1e6a0ffbd9fc915948434423a7d5,如何通过编程将其扩展回原始URL?显然,我可以使用像expandurl.com这样的API,但这将限制我每小时只能进行100个请求。
301
或302
,请查找Location
头部,并获取其中包含的“扩展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"];
}
成功找到了答案。
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());