.NET/C# 万能 HTTP 模拟请求框架
我是一名 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
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();
}
}
}
分类:
.NET Core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能