使用 .NET HttpClient 下载 PDF 文件的DEMO

使用 .NET HttpClient 下载 PDF 文件的DEMO使用GET方式下载PDF文件示例代码:usingSystem;namespaceConsoleApp2{classProgram{staticvoidMain(string[]args){System.Net.Http.HttpClienthttpClient=newSystem….

使用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);
                }
            }
        }
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/27317.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注