无法使用.NET重定向此URL

时间:2016-05-13 12:09:39

标签: c# .net vb.net http httpwebrequest

我想在运行时重定向下一个指向 RapidGator.Net 网址的网址:

  

http://r.plixid.com/r/ttPXzdeKwh0sfj2GWGfRC7L9O21r1ReLf2qeVlTXshrh91fikTYJbROyL9cuTNeu-qEDByV0epF-aCy-iYtbFzWqKju1wWeMJlKdeC_xoISJWCBL29PdmzUgQxmuQlseKk0rweOAxp0be60xDq7vqOrXV6mCEIBp2LYtnqTFJlw4-PhfL2pXuPN4NP1pRm3Zuk43Q6-ay7Qiyf3SBlqsS6gqG-23wwkR9YImE6HtSRyinp1N0qwVf-_tK1suG2ev

问题在于,当我尝试使用以下代码解决问题时,System.Net.WebException会因(500) Internal Server异常消息而导致响应失败。

VB.NET:

Public Shared Function RedirectUrl(ByVal url As String,
                                   Optional ByVal count As Integer = 1) As String

    Dim request As WebRequest

    For x As Integer = 1 To count
        request = HttpWebRequest.Create(url)
        Using response As WebResponse = request.GetResponse()
            If response.ResponseUri.AbsoluteUri.Equals(url) Then
                Exit For
            Else
                url = response.ResponseUri.AbsoluteUri
            End If
        End Using
    Next x

    Return url

End Function

C#:

public static string RedirectUrl(string url, int count = 1) {
    WebRequest request = default(WebRequest);
    for (int x = 1; x <= count; x++) {
        request = HttpWebRequest.Create(url);
        using (WebResponse response = request.GetResponse()) {
            if (response.ResponseUri.AbsoluteUri.Equals(url)) {
                break; // TODO: might not be correct. Was : Exit For
            } else {
                url = response.ResponseUri.AbsoluteUri;
            }
        }
    }
    return url;
}

我尝试解析指向 novafile.com 网址的其他网址,在这种情况下,我可以成功重定向。

  

http://r.plixid.com/r/JS5CYuD8rZJkU0spAuekKCnUNR63kfnDsJn_YOP8uuoyBuNB-SBB4M9Ji6qS7xp7-InjyqlPySAG1lGm93wrjyIORDehlV6qNweipO8VmQJA7fM1gm3Lz10_fIjZBuKqEU3zQNHOPpKJI8K980o-3Q ..

那么为什么我不能重定向指向 RapidGator.net 的网址,以及如何修复它(在C#或VB.NET中)?。

1 个答案:

答案 0 :(得分:1)

rapidgator的问题在于使用正确的标题。您可以使用WebClient或HttpWebRequest并尝试使用正确的标头或只是模仿UserAgent并且它可以正常工作

享受!

    public static string RedirectUrl(string url, int count = 1)
    {
        HttpWebRequest request; //Was WebRequest

        for (int x = 1; x <= count; x++)
        {
            request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            using (WebResponse response = request.GetResponse())
            {
                if (response.ResponseUri.AbsoluteUri.Equals(url))
                {
                    break; // TODO: might not be correct. Was : Exit For
                }
                else
                {
                    url = response.ResponseUri.AbsoluteUri;
                }
            }
        }
        return url;
    }