星空网 > 软件开发 > ASP.net

使用.Net中的HttpWebRequest类和HttpWebResponse类获取web文件

string url = "http://pic.maizuo.com/usr/100002174/0e495857e71e57b9486aecec788d557e.tmp";
WebRequest wreq = WebRequest.Create(url);
WebResponse wres = wreq.GetResponse();
long len = wres.ContentLength;
byte[] bytes=new byte[len];
Stream ss = wres.GetResponseStream();
string path = Server.MapPath("~/images/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
int idx = url.LastIndexOf(".");
string suffix = url.Substring(idx);//获得上传的图片的后缀名
if (suffix.Contains("tmp"))
{
suffix = ".jpg";
}
string pictureName = DateTime.Now.Ticks.ToString() + suffix;
FileStream os = new FileStream(path + pictureName, FileMode.OpenOrCreate, FileAccess.Write);
int c = 0;
while ((c = ss.Read(bytes, 0, bytes.Length)) > 0)
{
os.Write(bytes, 0, c);
}
os.Close();
ss.Close();




原标题:使用.Net中的HttpWebRequest类和HttpWebResponse类获取web文件

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流