Https 文件下载并转BASE64文件 -- C#

            System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
            httpClient.BaseAddress = new Uri("https://xxx.cn/");

            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://xxxxx.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;

                    byte[] buffer64 = new byte[stream.Length];

                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Read(buffer64, 0, (int)stream.Length);

                    var bast64File = Convert.ToBase64String(buffer64);
            }
posted @ 2022-06-06 10:42  三弄梅花  阅读(529)  评论(0编辑  收藏  举报