Search code examples
c#.netvb.nethttphttpwebrequest

Can't redirect this url using .NET


I would like to redirect at runtime the next url that points to a RapidGator.Net url:

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

The problem is that when I try to resolve it with the following code, a System.Net.WebException is thrown getting a response failure with (500) Internal Server exception message.

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;
}

I tried to resolve a different url that points to a novafile.com url, and in this case I can successfully redirect it.

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

Then why I can't redirect the url that points to RapidGator.net, and how I can fix it (in C# or VB.NET)?.


Solution

  • The problem with rapidgator is with proper headers. You could either user WebClient or HttpWebRequest and try playing around with proper headers or just impersonate UserAgent and it will work

    Enjoy!

        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;
        }