413请求实体对于HttpClient太大
问题描述:
一旦我通过PostAsJsonAsync发送HttpClient请求,我得到的请求实体响应太大。但我可以直接调用webapi并发送请求并返回成功响应。但通过PostAsJsonAsync它会返回413错误代码。413请求实体对于HttpClient太大
这是我的代码
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;
答
问题对服务器侧的待解(自托管HttpSelfHostServer或IIS)。
缓冲区必须设置为更高的值。
如果主机运行的IIS下: Configure 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