我需要为 HttpClient 配置 TCP 连接超时。目前,我正在使用用于整个请求的正常超时,但这还不足以正确控制应用程序的 POST。
Using client As New HttpClient()
Dim bArrayCredentials As Byte() = Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings("LDAPUser") & ":" & ConfigurationManager.AppSettings("LDAPPass"))
client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", Convert.ToBase64String(bArrayCredentials))
client.Timeout = TimeSpan.FromSeconds(CDbl(ConfigurationManager.AppSettings("TimeoutResponseLoginTVS")))
client.BaseAddress = New Uri(ConfigurationManager.AppSettings("URLLogin"))
Dim peticion = JsonConvert.SerializeObject(objPeticion)
Dim content As New StringContent(peticion, System.Text.Encoding.UTF8, "application/json")
Dim response As HttpResponseMessage = client.PostAsync(page, content).Result
If IsResponseValid(response.StatusCode) Then
Dim result As String = response.Content.ReadAsStringAsync().Result
Return result
End If
Return Nothing
End Using
¿有没有办法为 HttpClient 配置 TCP 连接超时?
如果最后一个问题无法实现,是否可以为 HttpWebRequest 配置 TCP 连接超时?
默认情况下,
httpclient 的超时应设置为 100 秒。
示例将超时设置为 5 分钟:
Dim Timeout As TimeSpan = TimeSpan.FromMinutes(5)
client.Timeout = Timeout