c# HttpClient每几个调用返回一次错误500(内部服务器错误)
本文关键字:错误 一次 服务器 内部 调用 HttpClient 几个 返回 | 更新日期: 2023-09-27 18:05:57
我的web应用程序使用HttpClient连接到后端。通常它可以工作,但是每10=15次调用就会出现一次错误500。当页面重新加载时,一切正常。错误出现在与HttpClient对象进行后端连接的代码中。我的web应用程序使用Unity作为IOC。
我在当前代码之前做了一些试验:HttpClientHelper有静态字段static HttpClient httpClient
,没有在代码
但仍然出现错误500。
BUT:当我做很多来自外部应用程序的调用,如提琴手,错误没有出现。我做了大约150个快速呼叫,没有出现任何错误。
UPDATE1:
我对fiddler的请求是:
GET http://www.zzz.com/methodA HTTP/1.1
Authorization: Basic **********************
Accept: application/json
Accept-Language: en-US, en; q=0.5
Host:www.zzz.com
fiddler的响应是:
HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 06 Sep 2016 14:06:34 GMT
Content-Length: 36
{"Message":"An error has occurred."}
更新2:我改变了代码,所以httpClient是一个静态属性。但问题还是一样的。HttpClientHelper类的代码是从async/await开始的,也许这是一个问题?
UPDATE3:我创建了一个新的帖子,它更详细:https://stackoverflow.com/questions/39367225/how-to-syncronize-static-valiable-inside-async-await-methods-httpclient-error-5欢迎你继续;)
我当前的连接类是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace MicrositeEngine.Common.Helpers
{
/// <summary>
/// Helper class for http calls
/// </summary>
public class HttpClientHelper
{
private IDictionary<string, string> _customHeaders { get; set; }
public HttpClientHelper() { }
public HttpClientHelper(IDictionary<string, string> customHeader)
{
_customHeaders = customHeader;
}
/// <summary>
/// Asychronously download string data from given url
/// </summary>
/// <param name="url">Place for download string from</param>
/// <returns>String data downloaded from url</returns>
public async Task<string> GetStringAsync(string url)
{
HttpResponseMessage response = null;
using (HttpClient httpClient = CreateHttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpRequestConsts.ContentTypeJson));
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
response = await httpClient.GetAsync(url).ConfigureAwait(true);
response.EnsureSuccessStatusCode();
}
return await response.Content.ReadAsStringAsync().ConfigureAwait(true);
}
/// <summary>
/// Asychronously download byte array data from given url
/// </summary>
/// <param name="url">Place for download byte array from</param>
/// <returns>Byte array data downloaded from url</returns>
public async Task<byte[]> GetByteArrayAsync(string url)
{
HttpResponseMessage response = null;
using (HttpClient httpClient = CreateHttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpRequestConsts.ContentTypeJson));
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
response = await httpClient.GetAsync(url).ConfigureAwait(true);
response.EnsureSuccessStatusCode();
}
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
}
private HttpClient CreateHttpClient()
{
var clientHandler = new HttpClientHandler { };
var client = new HttpClient(clientHandler);
if (_customHeaders != null)
{
foreach (var item in _customHeaders)
{
client.DefaultRequestHeaders.Add(item.Key, item.Value);
}
}
client.MaxResponseContentBufferSize = 2147483647; //max possible value
return client;
}
}
}
尝试使用HttpWebRequest而不是HttpClient。
也检查fiddler/wireshark的响应传出http请求