一旦我通过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;
答案 0 :(得分:0)
问题必须在服务器 -side(自托管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