我是一名 asp.net 程序员,专注于 b/s 项目开发。累计文章阅读量超过一千万,我的博客主页地址:

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
        {
            /// <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();
        }
    }
}