我有一个restsharp客户端,请求设置如下:
var request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
request.Timeout = -1;
request.ReadWriteTimeout = -1;
var url = $"http://{ipAddress}/api/calculate";
var client = new RestClient();
client.BaseUrl = new Uri(url);
client.Timeout = -1;
client.ReadWriteTimeout = -1;
var response = client.Execute(request);
此请求需要一段时间才能完成,大约需要 30 分钟。现在,我知道有更优雅的方法可以做到这一点,但是,对于这个请求,我需要这样做。
此 RestSharp 客户端和请求在 Windows 服务内部执行。当服务执行请求时,会抛出 TimoutException ,请求最大超时时间约为 40 秒。
由于某种原因,我设置的超时不适用于这种情况。
有人有解决办法吗?
var 选项 = new RestClientOptions("baseURL") { 抛出任何错误= true, MaxTimeout = 1000 // 1 秒 - 感谢@Scholtz }; var client = new RestClient(选项);
var options = new RestClientOptions("baseURL") {
ThrowOnAnyError = true,
Timeout = 1000 // 1 second - thanks to @JohnMc
};
var client = new RestClient(options);
将默认超时更改为:5 秒 - 例如 -(即 5000 毫秒):
var client = new RestClient("BaseUrl");
client.Timeout = 5000; // 5000 milliseconds == 5 seconds
通过设置
ReadWriteTimeout
值,您可能没有按照您的想法行事。您的值将被忽略,因此您将获得默认值。
根据这个答案RestSharp RestClient的默认超时值是多少?RestSharp在其实现中使用
HttpWebRequest
。
HttpWebRequest
的超时属性不能为负数HttpWebRequest.Timeout属性。
如果您查看 RestSharp 客户端代码,您会看到以下内容:https://github.com/restsharp/RestSharp/blob/70de357b0b9dfc3926c95d1e69967c7a7cbe874c/RestSharp/RestClient.cs#L452
int readWriteTimeout = request.ReadWriteTimeout > 0
? request.ReadWriteTimeout
: this.ReadWriteTimeout;
if (readWriteTimeout > 0)
{
http.ReadWriteTimeout = readWriteTimeout;
}
更新至 RestSharp v106.2.2。
请参阅https://github.com/restsharp/RestSharp/issues/1093