出于某种原因,当使用HttpClient和HttpWebRequest执行相同的POST请求时,HTTPClient需要2000 + ms,而HttpWebRequest需要大约1000-1400 ms。 这种差异是否正常,我能做些什么来使httpclient与httpwebrequest的速度相匹配?
我发布了以下代码,以防我搞砸了。
protected WebRequest CreateWebRequest(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(EndPoint));
request.Method = "POST";
request.ContentType = "application/json-rpc";
request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8");
request.Headers.Add(CustomHeaders);
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.ServicePoint.Expect100Continue = false;
ServicePointManager.Expect100Continue = false;
return request;
}
private async Task<string> DoHttpClientPost(string method, IDictionary<string, object> args = null)
{
{
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
handler.Proxy = null;
HttpResponseMessage response;
using (var myHttpClient = new HttpClient(handler))
{
myHttpClient.DefaultRequestHeaders.ExpectContinue = false;
myHttpClient.DefaultRequestHeaders.Add("Accept-Charset", "ISO-8859-1,utf-8");
myHttpClient.DefaultRequestHeaders.Add(APPKEY_HEADER, CustomHeaders.GetValues(APPKEY_HEADER));
myHttpClient.DefaultRequestHeaders.Add(SESSION_TOKEN_HEADER, CustomHeaders.GetValues(SESSION_TOKEN_HEADER));
myHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json-rpc"));
var call = new JsonRequest { Method = method, Id = 1, Params = args };
var jsonObject = JsonConvert.Serialize<JsonRequest>(call);
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json-rpc");
response = await myHttpClient.PostAsync(new Uri(EndPoint), content);
}
Console.WriteLine("\nCalling: " + method + " With args: " + JsonConvert.Serialize<IDictionary<string, object>>(args));
string jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse;
}
}
public async Task<T> Invoke2<T>(string method, IDictionary<string, object> args = null)
{
if (method == null)
throw new ArgumentNullException("method");
if (method.Length == 0)
throw new ArgumentException(null, "method");
string jsonString = await DoHttpClientPost(method, args);
var jsonResult = JsonConvert.Deserialize<JsonResponse<T>>(jsonString);
return jsonResult.Result;
}
public async Task<T> Invoke<T>(string method, IDictionary<string, object> args = null)
{
if (method == null)
throw new ArgumentNullException("method");
if (method.Length == 0)
throw new ArgumentException(null, "method");
var request = CreateWebRequest(new Uri(EndPoint));
using (Stream stream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
var call = new JsonRequest { Method = method, Id = 1, Params = args };
JsonConvert.Export(call, writer);
}
Console.WriteLine("\nCalling: " + method + " With args: " + JsonConvert.Serialize<IDictionary<string, object>>(args));
using (WebResponse response = GetWebResponse(request))
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
var jsonResponse = JsonConvert.Import<T>(reader);
// Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
if (jsonResponse.HasError)
{
throw ReconstituteException(jsonResponse.Error);
}
else
{
return jsonResponse.Result;
}
}
}
答案 0 :(得分:0)
如果可能,HttpClient应为reused across requests。您是否尝试过隔离创建HttpClient实例并执行实际请求?创建和处理实例所拥有的资源确实需要时间,尽管1000毫秒听起来很多。