我想在运行时重定向下一个指向 RapidGator.Net 网址的网址:
问题在于,当我尝试使用以下代码解决问题时,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 网址的其他网址,在这种情况下,我可以成功重定向。
那么为什么我不能重定向指向 RapidGator.net 的网址,以及如何修复它(在C#或VB.NET中)?。
答案 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;
}