2017-08-24 171 views
0

一旦我通過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; 

回答

0

問題對服務器側的待解(自託管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