开发者

webclient download problem

if this problem was discussed before,sorry for asking again..

I want to download an image from an url with using System.Net.WebClient class.

When i try to download an image (ie. google logo).it does 开发者_JAVA技巧not occur any errors,but some images are occurring errors.I dont understand why this errors.

how can i fix this problem?

my Code is:

WebClient client = new WebClient();
    try
    {
        //Downloads the file from the given url to the given destination                                         
        client.DownloadFile(urltxt.Text, filetxt.Text);
        return true;
    }
    catch (WebException w)
    {
        MessageBox.Show(w.ToString());
        return false;
    }
    catch (System.Security.SecurityException)
    {
        MessageBox.Show("securityexeption");
        return false;
    }
    catch (Exception)
    {
        MessageBox.Show("exception");
        return false;
    }

Errors are:

System.Net.WebException:The underlying connection was closed:An unexpected error occurred on a recieve.-->System.IO.IOException:Unable to read data from the transport connection:An existing connection was forcibly closed by the remote host.-->System.Net.Sockets.SocketException:An existing connection was forcibly closed by the remote host...bla bla

Thanks for your help.


You'd have to fire up something like Fiddler to see what is happening at the transport layer, but IMO it sounds like maybe you've been hitting them too much and the server has cut you off for a while (yes: many sites will do this if you hit them aggressively i.e. via a script).

In particular, since you mention google: see the terms of service (emphasis is mine):

5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.

It is also fairly common for sites to block access to images based on the referer http header, to discourage cross-site embedding of their image in your site.


The reason for getting this error could be network related or the client computer cannot access the remote image (network problem, firewall, ...) and has nothing to do with .NET, WebClient or programming. The remote site could also be throttling access if you are trying to send many requests from the same IP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜