实测 c# .net 中 httpwebrequest 和 httpclient 性能 区别 对比

来源:转载




实测 c# .net 中 httpwebrequest 和 httpclient 性能区别


以下是httpclient的代码


using (var http = new HttpClient())


{

//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary()
{
{"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访问数


分享给朋友:
您可能感兴趣的文章:
随机阅读: