.NET/C# 萬能 HTTP 模擬請求框架

我是一名 ASP.NET 程序員,專一於 B/S 項目開發。累計文章閱讀量超過一千萬,個人博客主頁地址:https://www.itsvse.com/blog_xzz.htmlphp

HttpHelper 介紹

HttpHelper 基於 netstandard 2.0 開發,支持.net 4.6.1和.net core項目,可以方便開發者發送 get 、post 請求,支持設置 cookie、header、代理等。內置將返回的json字符串轉換成對象。html

Demo

新建了一個 .net 4.6.1 的項目,低於該框架的將不支持。程序員

nuget命令以下:json

Install-Package Sw.Core.HttpHelper -Version 1.0.0

demo功能:get請求獲取源碼、測試post提交、獲取圖片、設置代理ip等。cookie

模擬http請求

設置 ip 代理訪問,以下圖:app

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
        {
            /// <summary>
            /// 
            /// </summary>
            public bool r { get; set; }
            /// <summary>
            /// 
            /// </summary>
            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<Root>().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();
        }
    }
}
相關文章
相關標籤/搜索