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

最佳答案

向 URL 发出请求并检查返回的状态代码。如果是 301302 ,请查找 Location header ,其中将包含“扩展 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"];
}

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

关于c# - 如何在 C# 中扩展 URL?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2569851/

10-11 22:53
查看更多