J'ai mis le timeout à 1 milliseconde, mais quand je teste, tous les appels http semblent fonctionner correctement, comme s'il ignorait le timeout. Comment faire pour que cela fonctionne ?
Gracias
private static HttpClient client = new HttpClient()
{
Timeout = TimeSpan.FromMilliseconds(1)
}; // re-use the object
/// <summary>
/// Make a GET call.
/// </summary>
public static async Task<string> GetAsync(string uri, string token_type, string access_token, string accept, string content_type)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
if (content_type != null) request.Headers.TryAddWithoutValidation("Content-Type", content_type);
if (accept != null) request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); // ACCEPT header
if (token_type != null && access_token != null) request.Headers.Authorization = new AuthenticationHeaderValue(token_type, access_token);
HttpResponseMessage g = await client.SendAsync(request);
if (g.IsSuccessStatusCode)
{
return await g.Content.ReadAsStringAsync();
}
else
{
throw new ExternalException(await g.Content.ReadAsStringAsync(), (int)g.StatusCode);
//throw new ExternalException(await g.Content.ReadAsStringAsync() + " Reason: " + g.ReasonPhrase + " Message: " + g.RequestMessage + " Status Code: " + g.StatusCode + " Token Type: " + token_type + " Content Type: " + content_type + " Accept: " + accept, (int)g.StatusCode);
}
}
/// <summary>
/// Make a POST call. The content type should be embedded in the postData already.
/// </summary>
public static async Task<string> PostAsync(string uri, string token_type, string access_token, HttpContent postData, string accept)
{
string mes = await postData.ReadAsStringAsync();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri)
{
Content = postData // CONTENT-TYPE header
};
if (accept != null) request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); // ACCEPT header
if (token_type != null && access_token != null) request.Headers.Authorization = new AuthenticationHeaderValue(token_type, access_token);
HttpResponseMessage g = await client.SendAsync(request);
if (g.IsSuccessStatusCode)
{
return await g.Content.ReadAsStringAsync();
}
else
{
throw new ExternalException(await g.Content.ReadAsStringAsync(), (int)g.StatusCode);
//throw new ExternalException(await g.Content.ReadAsStringAsync() + " Reason: " + g.ReasonPhrase + " Message: " + g.RequestMessage + " Status Code: " + g.StatusCode + " Token Type: " + token_type + " Post Data: " + mes + " Accept: " + accept, (int)g.StatusCode);
}
}