zoukankan      html  css  js  c++  java
  • 并发请求工具

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



    截图如下:

    image.png

    源码:http_request_tool

  • 相关阅读:
    mysql 需要掌握的重点
    Java基础知识之常见关键字以及概念总结
    abstract类中method
    java异常继承何类,运行时异常与一般异常的区别
    Java关键字final、static使用总结
    JAVA读取XML文件
    关于ApplicationContext的初始化
    web.xml配置详解
    maven javaProject打包发布成服务
    Spring Boot Actuator 配置和应用
  • 原文地址:https://www.cnblogs.com/wugang/p/14232344.html
Copyright © 2011-2022 走看看