实测 c# .net 中 httpwebrequest 和 httpclient 性能 区别 对比
2017-10-06 16:53
2061 查看
实测 c# .net 中 httpwebrequest 和 httpclient 性能区别
以下是httpclient的代码
using (var http = new HttpClient())
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"token", steptoken},
{"id", steporderid},
{"driverId", stepdriverid}
});
s_totalwebrequest0++;
var response = await http.PostAsync("http://" + s_webapipro + "/denyOrder", content);
string res = await response.Content.ReadAsStringAsync();
s_totalwebrequest1++;
JObject obj = JObject.Parse(res);
}
以下是httpwebrequest的代码
string url = "http://" + GetWebApiPro() + "/denyOrder";
string postData = "token=" + steptoken + "&id=" + steporderid + "&driverId=" + stepdriverid;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest.Method = "post";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
System.IO.Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string res = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
JObject obj = JObject.Parse(res);
httpwebrequest配合 如下 配置代码,将会提升客户端的并发能力
ServicePointManager.DefaultConnectionLimit = 1024;//提升系统外联的最大并发web访问数
然后在同样的程序环境中使用,都使用如下task异步线程池的方式调用
Task.Run(() =>
{
//上述代码段,其中httpclient需要async修饰符
}
实测效果 httpwebrequest 可以达到4000QPS峰值,2000到3000QPS均值(如果服务器有这么快),相同的访问环境,httpclient只有500平均,800峰值。
结论,httpwebrequest写web客户端或者并发度高的搜索爬虫类软件,效果远好于httpclient。
欢迎批评指正。
以下是httpclient的代码
using (var http = new HttpClient())
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"token", steptoken},
{"id", steporderid},
{"driverId", stepdriverid}
});
s_totalwebrequest0++;
var response = await http.PostAsync("http://" + s_webapipro + "/denyOrder", content);
string res = await response.Content.ReadAsStringAsync();
s_totalwebrequest1++;
JObject obj = JObject.Parse(res);
}
以下是httpwebrequest的代码
string url = "http://" + GetWebApiPro() + "/denyOrder";
string postData = "token=" + steptoken + "&id=" + steporderid + "&driverId=" + stepdriverid;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest.Method = "post";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
System.IO.Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string res = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
JObject obj = JObject.Parse(res);
httpwebrequest配合 如下 配置代码,将会提升客户端的并发能力
ServicePointManager.DefaultConnectionLimit = 1024;//提升系统外联的最大并发web访问数
然后在同样的程序环境中使用,都使用如下task异步线程池的方式调用
Task.Run(() =>
{
//上述代码段,其中httpclient需要async修饰符
}
实测效果 httpwebrequest 可以达到4000QPS峰值,2000到3000QPS均值(如果服务器有这么快),相同的访问环境,httpclient只有500平均,800峰值。
结论,httpwebrequest写web客户端或者并发度高的搜索爬虫类软件,效果远好于httpclient。
欢迎批评指正。
相关文章推荐
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- [转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
- C#: Create a WebRequest with HTTPClient
- [保存]C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- c# .net core HttpWebRequest 使用Headers[]方法代替UserAgent
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- C# .net WebRequest HttpWebRequest 禁用系统默认代理。
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- WebClient, HttpClient, HttpWebRequest ,RestSharp之间的区别与抉择
- 转:C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现