C# Http下载指定文件存储到特定目录,下面是标准用法代码:
-
///
-
/// Http下载文件:下载指定文件存储到特定目录
-
///
-
public static string HttpDownloadFile(string remoteUrl)
-
{
-
// 设置参数
-
HttpWebRequest request = WebRequest.Create(remoteUrl) as HttpWebRequest;
-
//发送请求并获取相应回应数据
-
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
-
//直到request.GetResponse()程序才开始向目标网页发送Post请求
-
Stream responseStream = response.GetResponseStream();
-
-
string localPath = AppDomain.CurrentDomain.BaseDirectory + @"QRCode\MyQRCode.jpg";
-
//创建本地文件写入流
-
Stream stream = new FileStream(localPath, FileMode.Create);
-
byte[] bArr = new byte[1024];
-
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
-
while (size > 0)
-
{
-
stream.Write(bArr, 0, size);
-
size = responseStream.Read(bArr, 0, (int)bArr.Length);
-
}
-
stream.Close();
-
responseStream.Close();
-
return localPath;
-
}
remoteUrl值说明:
remoteUrl的值举例
上面的URL值放到浏览器是可以直接打开文件的:
运行效果:
参考文献:
阅读(7150) | 评论(0) | 转发(0) |