WebClient 或 WebRequest 获取着陆页的重定向 URL

WebClient or WebRequest to get the re-directed URL of the landing page

从我从Bing's Pic of the Day解析出来的字符串中,我得到了要下载的图片的信息,假设今天是/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567,那么我们将有完整的URL图像就像 http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1366x768.jpg

通常Bing有更高分辨率的图片,所以我也会下载1920x1200的图片。很容易把URL改成http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1920x1200.jpg,然后把任务交给WebClient,比如client1.DownloadFile(url, fileName)

这里的问题是,有些日子,分辨率 1920x1200 不可​​用,并且此分辨率 (1920x1200) 的下载 URL 将被重定向到图像的 URL /sa/simg/hpb/NorthMale_EN-US8782628354_1920x1200.jpg - 默认(你可以查看)。

所以我的尝试是从输入 URL:

中获取 return/re-directed URL 的函数
 Public Function GetWebPageURL(ByVal url As String) As String
    Dim Request As WebRequest = WebRequest.Create(url)
    Request.Credentials = CredentialCache.DefaultCredentials
    Return Request.RequestUri.ToString 
 End Function

并与输入 URL 进行比较以查看它们是否不同,但结果与预期不同。

任何人都可以告诉我检查此重定向 URL 的方法,例如 return URL 在我们按 Enter 并等待站点加载后。

请给我一些克服这个障碍的想法。谢谢!

注意事项: 不同PC访问权限的一些问题导致我没有使用HttpWebRequest,所以我更喜欢不使用HttpWebRequestWebClient或其他更好)。


在@IvanValadares @AlenGenzić 的帮助下,以及@Jimi 对 HttpWebRequestProxy 的建议,我找到了公平的解决方案,如下面的代码:

    url1 = "http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1920x1200.jpg"

    Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url1), HttpWebRequest)
    myHttpWebRequest.MaximumAutomaticRedirections = 1
    myHttpWebRequest.AllowAutoRedirect = True
    Dim defaultProxy As IWebProxy = WebRequest.DefaultWebProxy
    If (defaultProxy IsNot Nothing) Then
        defaultProxy.Credentials = CredentialCache.DefaultCredentials
        myHttpWebRequest.Proxy = defaultProxy
    End If

    Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
    url2 = myHttpWebResponse.ResponseUri.ToString

    Label1.Text = url1
    Label2.Text = url2

使用 AllowAutoRedirect 并检查 StatusCode。

var webRequest = (HttpWebRequest)System.Net.WebRequest.Create("http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1920x1200.jpg");
    webRequest.AllowAutoRedirect = false;
    using (var response = (HttpWebResponse)webRequest.GetResponse())
    {
       if (response.StatusCode == HttpStatusCode.Found)
       {
          // Have been redirect
       }
       else if (response.StatusCode == HttpStatusCode.OK)
       {
          // Have not been redirect
       }
    }

使用 HttpClient

 var handler = new HttpClientHandler()
 {
    AllowAutoRedirect = false
 };
 HttpClient client = new HttpClient(handler);
 HttpResponseMessage response = await client.GetAsync("http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1920x1200.jpg");
 if (response.StatusCode == HttpStatusCode.Found)
 {
     // Have been redirect
 }
 else if (response.StatusCode == HttpStatusCode.OK)
 {
     // Have not been redirect
 }

在@IvanValadares @AlenGenzić 的帮助下,以及@Jimi 对 HttpWebRequestProxy 的建议,我得出了如下公平的解决方案:

    url1 = "http://www.bing.com/az/hprichbg/rb/PearlHarborWindows_EN-US8565186567_1920x1200.jpg"

    Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url1), HttpWebRequest)
    myHttpWebRequest.MaximumAutomaticRedirections = 1
    myHttpWebRequest.AllowAutoRedirect = True
    Dim defaultProxy As IWebProxy = WebRequest.DefaultWebProxy
    If (defaultProxy IsNot Nothing) Then
        defaultProxy.Credentials = CredentialCache.DefaultCredentials
        myHttpWebRequest.Proxy = defaultProxy
    End If

    Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
    url2 = myHttpWebResponse.ResponseUri.ToString

    Label1.Text = url1
    Label2.Text = url2

不再抛出System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.