简体   繁体   中英

C# Webclient DownloadFile 503 Error

Below is all the code that really needs to work. I have reason to believe this issue is only with discogs. If it is discogs, I would like to know a way around it such as emulating a browser.

Using the below code it should connect to the page and then download the image, but instead when I run it gets a 503 error. If I connect to the page with the same link it will show the image in my browser.

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");

however, if I use Imgur, and do this with the program it downloads the image as expected.

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

I am able to connect to discogs using the client, and even run a search through it and get the results as text (albeit through html) with client.DownloadString("url");

but I cannot download any images. I would appreciate any help with this, all I want are images at this point.

You can add an user-agent in header pretending that it is a request from a browser.

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");

I have reason to believe this issue is only with discogs.

You should have, in my opinion. There is no reason to believe that the issue is on your side, when a server properly returns 503 Service Unavailable result, and no exceptions are thrown.

Why does it happen? Only discogs developers may surely know.
My suggestion is that this service tries to avoid excessive non-user-generated traffic and returns this response instead of your image due to some Accept , User Agent or other client properties. That's why it only happens when you download it using WebClient. Try to modify something and pretend to be a user, not an application.

If you work through a proxy, you may also want to check it with direct connection.

The solution with add some header information (accept or user-agent) does not work for me.

But if I use HttpClient with a static instance it's working!

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

Although the solution from https://stackoverflow.com/users/1966464/germansniper didn't work for me exactly as it is, it pointed me in the right direction. My own version for someone else in the future...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM