tschuege
tschuege

Reputation: 761

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).

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.

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). If I set a timeout on a request to another endpoint on Exosites which does not check on it it works fine. Sending the exact same request without the "Request-Timeout" header also works fine.

Does anyone have experience with long polling calls in Xamarin using HttpClient?

Thanks a lot!

tschuege

Upvotes: 4

Views: 11127

Answers (2)

tschuege
tschuege

Reputation: 761

I finally figured out that "Request-Timeout" is not a standard header. It's a custom header Exosite uses to distinguish long polls from normal calls.

The problem I had was not related to that. I used fiddler to capture a long poll to the Exosite api made using postman. After the time defined with "Request-Timeout" passed by the response returned and Fiddler showed the following protocol violation:

Content-Length mismatch: Response Header indicated 27 bytes, but server sent 0 bytes.

So the problem was that HttpClient was not able to process the response. Fortunately SendAsync has an overload accepting an additional parameter of type HttpCompletionOption that has the following description on 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.

Using HttpCompletionOption.ResponseHeadersRead the call gets back before the response is read which worked like a charm in my case.

Upvotes: 2

Chase Florell
Chase Florell

Reputation: 47437

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.

Upvotes: 8

Related Questions