并发请求工具
使用C#基于.netframework4.6开发的并发请求工具
可以自定义Header,支持cookie。
支持GET/POST。
核心的HttpHelper代码:
public class HttpHelper { static public async Task Post(string url, Dictionary<string, string> headers, string body, DecompressionMethods decompress = DecompressionMethods.GZip, Action<string, int> callback = null, int index = 0) { try { var header = GetObj(headers); var handler = new HttpClientHandler() { UseCookies = false, AutomaticDecompression = decompress }; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(body, Encoding.UTF8, header.ContentType) }; SetHead(req.Headers, headers); HttpClient client = new HttpClient(handler); if (header.UserAgent != null) client.DefaultRequestHeaders.Add("User-Agent", header.UserAgent); if (header.Referer != null) client.DefaultRequestHeaders.Add("Referer", header.Referer); if (header.Origin != null) client.DefaultRequestHeaders.Add("Origin", header.Origin); if (header.AcceptLanguage != null) client.DefaultRequestHeaders.Add("Accept-Language", header.AcceptLanguage); if (header.Connection != null) client.DefaultRequestHeaders.Connection.Add(header.Connection); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Accept)); var res = await client.SendAsync(req); var str = await res.Content.ReadAsStringAsync(); if (callback != null) callback(str, index); } catch (Exception ex) { if (callback != null) callback(ex.Message, index); } } static public async Task Get(string url, Dictionary<string, string> headers, DecompressionMethods decompress = DecompressionMethods.GZip, Action<string, int> callback = null, int index = 0) { try { var header = GetObj(headers); var handler = new HttpClientHandler() { UseCookies = false, AutomaticDecompression = decompress }; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, url); SetHead(req.Headers, headers); HttpClient client = new HttpClient(handler); client.DefaultRequestHeaders.Add("User-Agent", header.UserAgent); var res = await client.SendAsync(req); var str = await res.Content.ReadAsStringAsync(); if (callback != null) callback(str, index); } catch (Exception ex) { if (callback != null) callback(ex.Message, index); } } private static void SetHead(HttpRequestHeaders headers, Dictionary<string, string> dics) { foreach (KeyValuePair<string, string> item in dics) { if (item.Key == "Content-Type") continue; if (item.Key == "Referer") continue; if (item.Key == "Content-Length") continue; if (item.Key == "Accept") continue; if (item.Key == "Connection") continue; if (item.Key == "Host") continue; headers.Add(item.Key, item.Value); } } private static HeaderObj GetObj(Dictionary<string, string> headers) { HeaderObj obj = new HeaderObj(); if (headers.ContainsKey("Content-Type")) { obj.ContentType = headers["Content-Type"]; } if (headers.ContainsKey("Referer")) { obj.Referer = headers["Referer"]; } if (headers.ContainsKey("Connection")) { obj.Connection = headers["Connection"]; } if (headers.ContainsKey("Accept")) { obj.Accept = headers["Accept"]; } if (headers.ContainsKey("User-Agent")) { obj.UserAgent = headers["User-Agent"]; } if (headers.ContainsKey("Origin")) { obj.Origin = headers["Origin"]; } if (headers.ContainsKey("Accept-Language")) { obj.AcceptLanguage = headers["Accept-Language"]; } return obj; } } class HeaderObj { public string ContentType { get; set; } = "application/x-www-form-urlencoded"; public string UserAgent { get; set; } = "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Mobile Safari/537.36"; public string Referer { get; set; } public string Connection { get; set; } public string Origin { get; set; } public string AcceptLanguage { get; set; } public string Accept { get; set; } }
截图如下: