c# - 网页客户端下载问题

标签 c# .net webclient

如果之前讨论过这个问题,抱歉再次提问..

我想使用 System.Net.WebClient 类从 url 下载图像。

当我尝试下载图片(即 google Logo )时。它没有出现任何错误,但有些图片出现错误。我不明白为什么会出现此错误。

我该如何解决这个问题?

我的代码是:

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

错误是:

System.Net.WebException:底层连接已关闭:接收时发生意外错误-->System.IO.IOException:无法从传输连接读取数据:现有连接被远程强行关闭host.-->System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭...bla bla

感谢您的帮助。

最佳答案

您必须启动诸如 Fiddler 之类的软件才能查看传输层发生了什么,但在我看来,听起来可能是您对它们的攻击太多了,服务器已将您切断关闭一段时间(是的:如果您通过脚本攻击它们,许多网站都会这样做)。

特别是,由于您提到了谷歌:请参阅 terms of service (重点是我的):

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.

网站根据 referer http header 阻止对图像的访问也很常见,以阻止在您的网站中跨站点嵌入他们的图像。

关于c# - 网页客户端下载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4539118/

相关文章:

c# - 在 DataGridView 中查找字符串

silverlight - 在 Silverlight 中使用 WebClient.BaseAddress 获取基本 url 的替代方法

c# - 在使用颜色和 double 之前反转它们的列表

.net - 如何延迟事件处理程序以首先运行一小段代码?

c# - 为什么插入 const 字符串会导致编译器错误?

c# - 有没有一种方法可以访问 .net 实体属性,而无需使用数组循环直接调用它?

c# - WebClient.DownloadString 首次调用时大约需要 15 秒

.net - PowerShell:System.Net.WebClient.DownloadFile 不会下载从 ListDirectory 收集的文件

c# - IEnumerable.Except 不起作用,那我该怎么办?

c# - ASPX/ASP.NET 中的空格控制