求助c#网络编程 想要实现把一条图片的uri包含的图片下载到本地。。求大佬指路。。脑细胞已经在地府排队了
如图。。api返回一条这种连接。。想下载下来。。但是httpclient还是不怎么会。。求大佬指条明路。。。谢谢补充下。。是UWP项目。。。DownloadFile好像用...
如图。。api返回一条这种连接。。想下载下来。。但是httpclient还是不怎么会。。求大佬指条明路。。。谢谢
补充下。。是UWP项目。。。DownloadFile好像用不了 QAQ 展开
补充下。。是UWP项目。。。DownloadFile好像用不了 QAQ 展开
1个回答
展开全部
public static bool HttpDownloadFile(string url, string path)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream responseStream = null;
Stream stream = null;
try
{
// 设置参数
request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 10000;
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
responseStream = response.GetResponseStream();
//创建本地文件写入流
stream = new FileStream(path, FileMode.OpenOrCreate);
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 true;
}
catch
{
return false;
}
finally
{
try
{
if (response != null) response.Close();
if (responseStream != null) responseStream.Dispose();
if (stream != null) stream.Dispose();
}
catch { }
}
}
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream responseStream = null;
Stream stream = null;
try
{
// 设置参数
request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 10000;
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
responseStream = response.GetResponseStream();
//创建本地文件写入流
stream = new FileStream(path, FileMode.OpenOrCreate);
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 true;
}
catch
{
return false;
}
finally
{
try
{
if (response != null) response.Close();
if (responseStream != null) responseStream.Dispose();
if (stream != null) stream.Dispose();
}
catch { }
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询