I would like to redirect at runtime the next url that points to a RapidGator.Net url:
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.
Then why I can't redirect the url that points to RapidGator.net, and how I can fix it (in C# or VB.NET)?.
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;
}