【问题标题】:C# Webclient DownloadFile 503 ErrorC# Webclient 下载文件 503 错误
【发布时间】:2017-03-09 14:18:41
【问题描述】:

下面是真正需要工作的所有代码。我有理由相信这个问题只存在于discogs。如果是discogs,我想知道一种解决方法,例如模拟浏览器。

使用下面的代码,它应该连接到页面,然后下载图像,但是当我运行它时却得到 503 错误。如果我使用相同的链接连接到页面,它将在我的浏览器中显示图像。

WebClient client = new WebClient();  
client.DownloadFile("https://img.discogs.com/h7oMsgLSWi7D6nBR8wdBwWulJ8w=/fit-in/600x600/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-9259029-1477514675-9740.jpeg.jpg", @"C:\Programming\Test\downloadimage.jpg");

但是,如果我使用 Imgur,并使用程序执行此操作,它会按预期下载图像。

client.DownloadFile("http://i.imgur.com/sFq0wAC.jpg", @"C:\Programming\Test\downloadimage.jpg");

我可以使用客户端连接到 discogs,甚至可以通过它运行搜索并使用 client.DownloadString("url"); 以文本形式(尽管是通过 html)获得结果;

但我无法下载任何图像。对此我将不胜感激,我现在​​想要的只是图像。

【问题讨论】:

    标签: c# download webclient server-error


    【解决方案1】:

    您可以在标头中添加用户代理,假装它是来自浏览器的请求。

    WebClient client = new WebClient();
    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");    
    client.DownloadFile("https://img.discogs.com/h7oMsgLSWi7D6nBR8wdBwWulJ8w=/fit-in/600x600/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-9259029-1477514675-9740.jpeg.jpg", @"C:\Programming\Test\downloadimage.jpg");
    

    【讨论】:

      【解决方案2】:

      我有理由相信这个问题只存在于discogs。

      在我看来,你应该有。当 a server 正确返回 503 Service Unavailable 结果并且没有抛出异常时,没有理由相信问题出在您这边。

      为什么会这样?只有 discogs 开发人员可能肯定知道。
      我的建议是,由于某些AcceptUser Agent 或其他客户端属性,此服务会尝试避免过多的非用户生成的流量并返回此响应而不是您的图像。这就是为什么它只在您使用 WebClient 下载它时发生。尝试修改某些内容并假装自己是用户,而不是应用程序。

      如果您通过代理工作,您可能还想通过直接连接进行检查。

      【讨论】:

      • 感谢您确认不是我。你建议我用什么来伪装成用户?
      【解决方案3】:

      添加一些标头信息(接受或用户代理)的解决方案对我不起作用。

      但如果我将 HttpClient 与静态实例一起使用,它就可以工作!

          private static HttpClient _httpClient = new HttpClient();
      
          public static async Task<Stream> LoadImageFromUrl(string url)
          {
              Stream stream = null;
              try
              {
                  HttpResponseMessage result = await _httpClient.GetAsync(url);
                  stream = await result.Content.ReadAsStreamAsync();
              }
              catch (Exception e)
              {
                  // TODO
              }
              return stream;
          }
      

      【讨论】:

        【解决方案4】:

        尽管https://stackoverflow.com/users/1966464/germansniper 的解决方案对我来说并没有完全按原样工作,但它为我指明了正确的方向。我自己的版本,以后给别人看……

        using (HttpClient http = new HttpClient { BaseAddress = new Uri(url) }) {
            http.DefaultRequestHeaders.Accept.Clear();
            http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/image"));
        
            HttpResponseMessage result = http.GetAsync(url).Result;
        
            if (result.IsSuccessStatusCode) {
                Stream stream = result.Content.ReadAsStreamAsync().Result;
                return Image.FromStream(stream);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-20
          • 1970-01-01
          • 1970-01-01
          • 2015-12-30
          • 2017-01-11
          相关资源
          最近更新 更多