简体   繁体   English

Xamarin HttpClient请求超时

[英]Xamarin HttpClient Request-Timeout

In a Xamarin.Forms application I try to connect to the Exosites api (which is not part of the project so I can't change that one so SignalR or so). 在Xamarin.Forms应用程序中,我尝试连接到Exosites api(这不是项目的一部分,因此,SignalR还是不能更改它)。

It all works fine for "normal" requests. 对于“正常”请求,一切正常。

The api also supports long polling requests - in the manual it says that the client has to set the header "Request-Timeout" to the request. 该api还支持长轮询请求-在手册中它说客户端必须为请求设置标头“ Request-Timeout”。

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
request.Headers.Add("Request-Timeout", "10000");
var response = await client.SendAsync(request);

But doing so, I don't get any answer back, even if i set the timeout to a very small value like 1 (ms). 但是这样做,即使我将超时设置为一个非常小的值(如1(ms)),也无法得到任何答案。 If I set a timeout on a request to another endpoint on Exosites which does not check on it it works fine. 如果我在对Exosites上的另一个端点的请求上设置了超时,但未对其进行检查,则可以正常工作。 Sending the exact same request without the "Request-Timeout" header also works fine. 发送没有“ Request-Timeout”标头的完全相同的请求也可以正常工作。

Does anyone have experience with long polling calls in Xamarin using HttpClient? 有没有人有使用HttpClient在Xamarin中进行长轮询呼叫的经验?

Thanks a lot! 非常感谢!

tschuege tschuege

Actually, it's a lot easier than that. 实际上,这要容易得多。

using (var client = new HttpClient())
{
    client.Timeout = TimeSpan.FromSeconds(_timeoutSeconds);
}

PS: Be sure to always wrap your new HttpClient() in a using block. PS:确保始终将new HttpClient()包装在using块中。

I finally figured out that "Request-Timeout" is not a standard header. 我终于发现“ Request-Timeout”不是标准的标头。 It's a custom header Exosite uses to distinguish long polls from normal calls. 这是Exosite使用的自定义标头,用于区分长时间轮询和普通呼叫。

The problem I had was not related to that. 我遇到的问题与此无关。 I used fiddler to capture a long poll to the Exosite api made using postman. 我用提琴手捕捉了对使用邮差制作的Exosite api的长时间调查。 After the time defined with "Request-Timeout" passed by the response returned and Fiddler showed the following protocol violation: 在返回的响应时间为“ Request-Timeout”定义的时间之后,Fiddler显示了以下协议冲突:

Content-Length mismatch: Response Header indicated 27 bytes, but server sent 0 bytes. 内容长度不匹配:响应标头指示27个字节,但服务器发送了0个字节。

So the problem was that HttpClient was not able to process the response. 因此,问题在于HttpClient无法处理响应。 Fortunately SendAsync has an overload accepting an additional parameter of type HttpCompletionOption that has the following description on msdn: 幸运的是, SendAsync具有重载,它接受类型为HttpCompletionOption的附加参数,该参数在msdn上具有以下描述:

Indicates if HttpClient operations should be considered completed either as soon as a response is available, or after reading the entire response message including the content. 指示是否应在响应可用后立即或在读取包括内容的整个响应消息之后认为HttpClient操作已完成。

Using HttpCompletionOption.ResponseHeadersRead the call gets back before the response is read which worked like a charm in my case. 使用HttpCompletionOption.ResponseHeadersRead调用会在读取响应之前返回,这在我的情况下就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM