1. 程式人生 > 其它 >Https 檔案下載並轉BASE64檔案 -- C#

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