无法使用ASP.NET中的WebClient下载图像
在其中一个移动站点中,我创建了一个网页,其中我使用webclient从主站点下载图像移动的主站点并使用位图调整大小,然后将图像获取到移动站点,主站点的图像路径工作正常,但当我使用webclient下载图像以调整大小时,我得到以下错误:无法使用ASP.NET中的WebClient下载图像,asp.net,sockets,webclient,webexception,Asp.net,Sockets,Webclient,Webexception,在其中一个移动站点中,我创建了一个网页,其中我使用webclient从主站点下载图像移动的主站点并使用位图调整大小,然后将图像获取到移动站点,主站点的图像路径工作正常,但当我使用webclient下载图像以调整大小时,我得到以下错误: CreateThumbnail :System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A co
CreateThumbnail :System.Net.WebException: Unable to connect to the
remote server ---> System.Net.Sockets.SocketException: A connection
attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond 209.59.186.108:80 at
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress) at
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
Socket s4, Socket s6, Socket& socket, IPAddress& address,
ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
Exception& exception)
有人能建议解决这个问题的方法吗?我尝试使用以下命令ping上述IP 209.59.186.108:
ping m.keyboardmag.com
其结果如下:
Pinging m.keyboardmag.com [209.59.186.108] with 32 byte
Reply from 209.59.186.108: bytes=32 time=233ms TTL=112
Reply from 209.59.186.108: bytes=32 time=237ms TTL=112
Reply from 209.59.186.108: bytes=32 time=230ms TTL=112
Reply from 209.59.186.108: bytes=32 time=231ms TTL=112
仍然无法使用WebClient连接和下载图像
*************更新的代码片段****************
您的移动站点的响应时间太长。从代码片段中可以看出,您显然正在访问端口80,因此,如果您可以使用浏览器访问您的站点,那么您的代码没有任何问题。您存在某种网络延迟问题 以防万一,做:
WebClient.DownloadFile("UrlToImage");
应该可以。你能发布一些示例代码吗 在过去,我使用了下面的代码,从来没有任何问题
WebClient wb = new WebClient();
Image originalImage = Image.FromStream(wb.OpenRead(Url));
Image thumbNail = ImageResize.Crop(originalImage, Width, Height, ImageResize.AnchorPosition.Top);
保存图像的代码:
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, MyQuality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
originalImage.Save(FilePath, jpegCodec, encoderParams);
MyQuality是介于0-100之间的整数。50-70是一个很好的起点,具体取决于您需要什么。请发布使用WebClient的代码。您好,我已经上传了我正在使用的WebClient代码片段,请检查编辑问题。您好@k_man,您能告诉我ImageResize类的名称空间是什么,还是自定义名称空间class@Abbas,对不起,我忘了我在那了。这是我在@Abbas找到的一个很好的助手类,在查看示例代码时,它看起来应该可以工作。我会尝试上面的代码。当我进行测试时,我会将原始图像保存到硬盘上,以验证我下载的内容是否正确,请参阅上面的保存代码。另外,我认为url应该使用url中的http://完全限定。如何检查站点是否正在访问端口80您正在访问端口80,请查看ip地址后的“:80”。这是端口号。同样,如果您可以使用浏览器访问该网站,那么您应该可以使用代码访问该网站。我觉得很好。你有代理人吗?在连接之前,您可能需要使用代理进行身份验证?WebClient有一个属性,请使用它,这是您的情况。@我也使用了身份验证,但仍然无法使其工作
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, MyQuality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
originalImage.Save(FilePath, jpegCodec, encoderParams);