如何在C#中扩展URL?

Hao*_*Lim 3 c# http httpwebrequest

如果我有http://popurls.com/go/msn.com/l4eba1e6a0ffbd9fc915948434423a7d5这样的网址,如何以编程方式将其扩展回原始网址?显然我可以使用像expandurl.com这样的API,但这会限制我每小时100个请求.

Jør*_*ode 7

请求URL并检查返回的状态代码.如果301302,查找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"];
}
Run Code Online (Sandbox Code Playgroud)

注意:此解决方案假定只发生一次重定向.这可能是也可能不是你想要的.如果您只想对来自混淆器(bit.ly等)的URL进行反混淆处理,则此解决方案应该可以正常工作.