我是一名 ASP.NET 程式員,專註於 B/S 項目開發。累計文章閱讀量超過一千萬,我的博客主頁地址:https://www.itsvse.com/blog_xzz.html HttpHelper 介紹 HttpHelper 基於 netstandard 2.0 開發,支持.net 4.6.1和. ...
我是一名 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
{
/// <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();
}
}
}