基础连接已经关闭: 连接被意外关闭
小弟网上DOWN了一段图片上传的代码,可是跑起来的时候遇到这样一个问题,
代码如下:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string path = txtImgUrl.Text;
downRemoteImg(path);
}
public void downRemoteImg( string imgpath)
{
if (string.IsNullOrEmpty(imgpath))
return;
else
{
string imgName = string.Empty;
string imgExt = string.Empty;
string saveFilePath = string.Empty;
if (imgpath.LastIndexOf("/")<0)
imgName = imgpath.Substring(imgpath.LastIndexOf("\\"), imgpath.Length - imgpath.LastIndexOf("\\"));
else
imgName = imgpath.Substring(imgpath.LastIndexOf("/"), imgpath.Length - imgpath.LastIndexOf("/"));
imgExt = imgpath.Substring(imgpath.LastIndexOf("."), imgpath.Length - imgpath.LastIndexOf("."));
try
{
WebRequest wreq = WebRequest.Create(imgpath);
wreq.Timeout = 10000;
FileWebResponse wresp = (FileWebResponse)wreq.GetResponse();
Stream s = wresp.GetResponseStream();
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(s);
switch (imgExt.ToLower())
{
case ".gif":
img.Save("C:\\files" + imgName, ImageFormat.Gif);
break;
case ".jpg":
case ".jpeg":
img.Save("C:\\files" + imgName, ImageFormat.Jpeg);
break;
case ".png":
img.Save("C:\\files" + imgName, ImageFormat.Png);
break;
case ".icon":
img.Save("C:\\files" + imgName, ImageFormat.Icon);
break;
case ".bmp":
img.Save("C:\\files" + imgName, ImageFormat.Bmp);
break;
}
img.Dispose();
s.Dispose();
}
catch
{
return;
}
}
}
上传本地的图片都可以上传成功,但是把URL换成网络上的图片时,红色代码就会报错,
"基础连接已经关闭: 连接被意外关闭。"
网上找了半天也没找到合适的答案,有.net高手知道怎么解决的话请指点下``
------解决方案--------------------
上传别人网站上的图片,只要把那个地址保存了就行了,假如那个网站不会关闭
------解决方案--------------------