.NET/C# 万能 HTTP 模拟请求框架

2021-01-20 04:14

阅读:428

标签:ext   lang   byte   ips   源码   result   tao   ica   summary   

我是一名 ASP.NET 程序员,专注于 B/S 项目开发。累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html

HttpHelper 介绍

HttpHelper 基于 netstandard 2.0 开发,支持.net 4.6.1和.net core项目,能够方便开发者发送 get 、post 请求,支持设置 cookie、header、代理等。内置将返回的json字符串转换成对象。

Demo

新建了一个 .net 4.6.1 的项目,低于该框架的将不支持。

nuget命令如下:

Install-Package Sw.Core.HttpHelper -Version 1.0.0

demo功能:get请求获取源码、测试post提交、获取图片、设置代理ip等。

技术图片

设置 ip 代理访问,如下图:

技术图片

代码如下:

using Sw.Core.HttpHelper;
using System;
using System.Threading;
using System.Windows.Forms;

namespace HttpHelper_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    var http = new HttpHelper();
                    var item = new HttpItem()
                    {
                        URL = "https://www.itsvse.com/"
                    };
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    var result = http.GetHtml(item);
                    watch.Stop();
                    if (result.StatusCode== System.Net.HttpStatusCode.OK)
                    {
                        base.Invoke(new Action(() =>
                        {
                            textBox1.Text = result.Html;
                        }));
                        RequestTime(watch.ElapsedMilliseconds);
                    }
                    
                }
                catch { }
            })
            { IsBackground = true }.Start();
        }

        private void RequestTime(long s)
        {
            base.Invoke(new Action(() =>
            {
                toolStripStatusLabel1.Text = $"执行耗时:{s}ms";
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    var http = new HttpHelper();
                    var item = new HttpItem()
                    {
                        URL = "https://down.itsvse.com/Account/ImgCode",
                        ResultType = Sw.Core.HttpHelper.Enum.ResultType.Byte
                    };
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    var result = http.GetHtml(item);
                    watch.Stop();
                    if (result.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        base.Invoke(new Action(() =>
                        {
                            textBox2.Text = result.Cookie;
                            pictureBox1.Image = HttpHelper.GetImage(result.ResultByte);
                        }));
                        RequestTime(watch.ElapsedMilliseconds);
                    }

                }
                catch { }
            })
            { IsBackground = true }.Start();
        }

        public class Root
        {
            /// 
            /// 
            /// 
            public bool r { get; set; }
            /// 
            /// 
            /// 
            public string m { get; set; }
        }


        private void button3_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    var http = new HttpHelper();
                    var item = new HttpItem()
                    {
                        URL = "https://down.itsvse.com/User/GetUserInfo"
                    };
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    var result = http.GetHtml(item);
                    watch.Stop();
                    if (result.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        base.Invoke(new Action(() =>
                        {
                            textBox1.Text = result.Html;
                            MessageBox.Show(result.JsonToObject().m);
                        }));
                        RequestTime(watch.ElapsedMilliseconds);
                    }

                }
                catch { }
            })
            { IsBackground = true }.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    string post = "UserName=111&Password=111&txtCode=111&RememberMe=true&language=zh-cn";
                    var http = new HttpHelper();
                    var item = new HttpItem()
                    {
                        URL = "https://down.itsvse.com/Account/Login",
                        Method = "POST",
                        ContentType = "application/x-www-form-urlencoded",
                        Postdata = post,
                    };
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    var result = http.GetHtml(item);
                    watch.Stop();
                    if (result.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        base.Invoke(new Action(() =>
                        {
                            textBox1.Text = result.Html;
                        }));
                        RequestTime(watch.ElapsedMilliseconds);
                    }
                    MessageBox.Show(result.StatusDescription);
                }
                catch { }
            })
            { IsBackground = true }.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    var http = new HttpHelper();
                    var item = new HttpItem()
                    {
                        URL = "http://ip.taobao.com/service/getIpInfo2.php",
                        Method = "POST",
                        ContentType = "application/x-www-form-urlencoded",
                        Postdata = "ip=myip",
                        ProxyIp = "47.106.124.179:80",
                    };
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    var result = http.GetHtml(item);
                    watch.Stop();
                    if (result.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        base.Invoke(new Action(() =>
                        {
                            textBox1.Text = result.Html;
                        }));
                        RequestTime(watch.ElapsedMilliseconds);
                    }
                }
                catch { }
            })
            { IsBackground = true }.Start();
        }
    }
}

.NET/C# 万能 HTTP 模拟请求框架

标签:ext   lang   byte   ips   源码   result   tao   ica   summary   

原文地址:https://www.cnblogs.com/itsvse/p/12145152.html


评论


亲,登录后才可以留言!