1

一旦我通过 PostAsJsonAsync 发送 HttpClient 请求,我得到的响应是请求实体太大。但我可以直接调用 webapi 并发送请求并返回成功的响应。但通过 PostAsJsonAsync 它返回 413 错误代码。

这是我的代码

    var client = new HttpClient { BaseAddress = new Uri(ConfigurationManager.AppSettings.Get("API_HOST")) };
    const string api = "CmSaveChange" + "/" + "SaveChange";
    var response = client.PostAsJsonAsync(api, entity).Result;
    var retunValue = response.Content.ReadAsAsync<HybridDictionary>().Result;
4

1 回答 1

1

该问题必须在服务器端(自托管 HttpSelfHostServer 或 IIS)解决。
缓冲区必须设置为更高的值。
如果主机在 IIS 下运行: 配置 IIS

如果服务器作为 HttpSelfHostServer 运行:
您必须为配置参数设置更高的值(根据需要)。
vb.net 的示例

Dim cSelhostConfiguration As String = cIPADressePort
' Note: cIPADressePort contains the IP address and port on which the host is listen
Dim config As New HttpSelfHostConfiguration(cSelhostConfiguration)
'Set here the needed size (in bytes)
config.MaxBufferSize = 250000000
config.MaxReceivedMessageSize = 250000000
'
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
'
Using server As New HttpSelfHostServer(config)
Try
server.OpenAsync().Wait()
Console.WriteLine("")
Console.WriteLine("WebService started... ")
Console.WriteLine("Waiting for work...")
  Catch aggEx As AggregateException
Console.WriteLine("Error loading Server")
  End Try
  Console.WriteLine()
Console.WriteLine("Press enter to close the server")
  Console.ReadLine()
End Using
于 2017-11-30T08:52:32.873 回答