使用GET方式下载PDF文件
示例代码:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
httpClient.BaseAddress = new Uri("https://dms.esnadexpress.com/");
System.Net.Http.HttpRequestMessage httpRequestMessage = new System.Net.Http.HttpRequestMessage();
httpRequestMessage.Method = new System.Net.Http.HttpMethod("GET");
httpRequestMessage.Headers.Add("context-type", "application/pdf");
httpRequestMessage.RequestUri = new Uri("https://dms.esnadexpress.com/img/2019-07-16/2/custom_pdf_ESNAD_1563242468479_54981_3223fe36-31c3-4d73-b447-d2c4720046fa.pdf");
System.Net.Http.HttpResponseMessage httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result;
if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
System.IO.Stream stream = httpResponseMessage.Content.ReadAsStreamAsync().Result;
using (System.IO.FileStream fs = new System.IO.FileStream("e:\\download.pdf",System.IO.FileMode.CreateNew))
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
}
}
}
}