实测 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配合 如下 配置代码,将会提升客户端的并发能力
{
//上述代码段,其中httpclient需要async修饰符
}