csharp c#HTTP请求帮助类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp c#HTTP请求帮助类相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class HttpHelper
    {
        public static async Task<string> HttpGetAsync(string url, bool isCook = true)
        {
            var baseAddress = new Uri(url);
            using (var httpClient = new HttpClient() { BaseAddress = baseAddress })
            {
                //var message = new HttpRequestMessage(HttpMethod.Get, baseAddress);
                //var message = new HttpRequestMessage(HttpMethod.Put, baseAddress);
                var message = new HttpRequestMessage(HttpMethod., baseAddress);
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "UTF-8");//("Accept-Encoding", "gzip, deflate"); //
                //httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Mobile Safari/537.36");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
                if (isCook) httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Cookie", "UUUXXX");

                var response = await httpClient.SendAsync(message);
                response.EnsureSuccessStatusCode();
                var resultModel = await response.Content.ReadAsByteArrayAsync();
                return Encoding.UTF8.GetString(resultModel);
            }
        }
    }
}

以上是关于csharp c#HTTP请求帮助类的主要内容,如果未能解决你的问题,请参考以下文章

C#Http帮助类

csharp 使用AddClipboardFormatListener / WM_CLIPBOARDUPDATE监视C#中的剪贴板更改。请参阅剪贴板类:http://msdn.microsoft.co

C# Http访问帮助类,支持get post请求文件下载 [

csharp Http Web请求

csharp Http请求拦截器

后台请求帮助类