2016-12-05 1 views
0

Aus irgendeinem Grund, wenn die gleiche POST-Anfrage mit HttpClient und HttpWebRequest, HTTPClient dauert 2000 + ms, während HttpWebRequest dauert etwa 1000-1400 ms. Ist diese Disparität normal und kann ich irgendetwas tun, damit der httpclient mit der Geschwindigkeit der httpwebrequest übereinstimmt?C# Langsame Antwortzeiten mit HttpClient im Vergleich zu HttpWebRequest

Ich habe den Code unten gepostet, falls ich etwas vermassle.

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

Haben Sie sich angeschaut, was im Netzwerk mit Wireshark oder Fiddler passiert? –

+0

Ja, sie haben die gleichen Header und haben die gleiche Größe, es ist sehr verwirrend. – itsdrea

Antwort

0

HttpClient sollte, wenn möglich, reused across requests sein. Haben Sie versucht, die HttpClient-Instanz zu isolieren und die eigentliche Anfrage auszuführen? Sowohl das Erstellen als auch das Disponieren der Ressourcen, die von der Instanz gehalten werden, brauchen Zeit, obwohl 1000ms viel klingen.