Asp.Net 核心 HttpClient 收到响应 411 长度要求错误
Asp.Net core HttpClient gets response 411 length required error
我正在尝试使用 C# HttpClient 创建一个将一些数据发布到 API 端点的服务。代码如下
public class HttpClientService : IHttpClientService
{
static HttpClient client = new HttpClient();
public HttpClientService()
{
client.BaseAddress = new Uri("http://xx.xx.xx.xx/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<Uri> MakeLogEntry(CnsLog log)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/logs", log);
return response.Headers.Location;
}
}
问题是终点returns错误411 Length Required。我发现这是因为我的请求没有 content-length header 集,我在使用 Fiddler 检查请求时发现这是真的。
我尝试在客户端的构造函数中设置内容长度 header,但此后代码无法编译。我被困住了,将不胜感激任何帮助。谢谢
或者您可以使用 HttpWebRequest
。
byte[] postBytes = Encoding.ASCII.GetBytes(log);
request.ContentLength = postBytes.Length;
HttpWebRequest 示例如下:
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes(serializedObject); // a json object, or xml, whatever...
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Expect = "application/json";
request.GetRequestStream().Write(data, 0, data.Length);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
您不想在客户端上设置 Content-Length
header,尤其是因为它是静态实例。您想根据个人要求进行设置。
PostAsJsonAsync
是一个很好的快捷方式,它从 poco 构建 HttpContent
,从该内容构建 HttpRequestMessage
,然后发送 POST 请求。很方便,但是所有这些抽象都不会给您设置 request-level header 的机会。因此,您需要为 build/send 请求做更多的工作:
var json = JsonConvert.SerializeObject(log);
var content = new StringContent(json, Encoding.UTF8, "application/json");
content.Headers.ContentLength = json.Length;
var response = await client.PostAsync("api/logs", content);
我正在尝试使用 C# HttpClient 创建一个将一些数据发布到 API 端点的服务。代码如下
public class HttpClientService : IHttpClientService
{
static HttpClient client = new HttpClient();
public HttpClientService()
{
client.BaseAddress = new Uri("http://xx.xx.xx.xx/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<Uri> MakeLogEntry(CnsLog log)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/logs", log);
return response.Headers.Location;
}
}
问题是终点returns错误411 Length Required。我发现这是因为我的请求没有 content-length header 集,我在使用 Fiddler 检查请求时发现这是真的。
我尝试在客户端的构造函数中设置内容长度 header,但此后代码无法编译。我被困住了,将不胜感激任何帮助。谢谢
或者您可以使用 HttpWebRequest
。
byte[] postBytes = Encoding.ASCII.GetBytes(log);
request.ContentLength = postBytes.Length;
HttpWebRequest 示例如下:
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes(serializedObject); // a json object, or xml, whatever...
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Expect = "application/json";
request.GetRequestStream().Write(data, 0, data.Length);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
您不想在客户端上设置 Content-Length
header,尤其是因为它是静态实例。您想根据个人要求进行设置。
PostAsJsonAsync
是一个很好的快捷方式,它从 poco 构建 HttpContent
,从该内容构建 HttpRequestMessage
,然后发送 POST 请求。很方便,但是所有这些抽象都不会给您设置 request-level header 的机会。因此,您需要为 build/send 请求做更多的工作:
var json = JsonConvert.SerializeObject(log);
var content = new StringContent(json, Encoding.UTF8, "application/json");
content.Headers.ContentLength = json.Length;
var response = await client.PostAsync("api/logs", content);