dotnet-httpclient

Retrying C# HttpClient Unsuccessful Requests and Timeouts

大兔子大兔子 提交于 2019-12-21 21:10:02
问题 I'm trying to build in retrying in an HttpClient DelegatingHandler such that responses such as 503 Server Unavailable and timeouts are treated as transient failures and retried automatically. I was starting from the code at http://blog.devscrum.net/2014/05/building-a-transient-retry-handler-for-the-net-httpclient/ which works for the 403 Server Unavailable case, but does not treat timeouts as transient failures. Still, I like the general idea of using the Microsoft Transient Fault Handling

HttpClient - how to figure out if server is down faster?

六眼飞鱼酱① 提交于 2019-12-21 17:28:09
问题 I'm using .NET HttpClient to send requests to my server. I have set HttpClient.Timeout property to 10 seconds so i'm getting a A task was cancelled exception whenever the server fails to process my request in less than 10 seconds. Good until here. However, if the server is shut down it takes for the HttpClient around ~20 seconds to return a proper exception such as the one in the picture. I would like to see this exception faster than 10 seconds to be able to distinguish between Server Down

.Net 4.0 HttpClient usage?

旧城冷巷雨未停 提交于 2019-12-21 09:17:36
问题 im in .Net 4.0 and attempting to use the HttpClient. I read some articles saying that it was no longer supported in 4.0 but that you could still use it? i've included the System.Net.Http; assembly but it's not allowing me to provide the necessary params to the HttpClient . Any idea how I could fix this? I've bolded where the errors are occuring. using (HttpClient http = new **HttpClient("{0}/v1/dm/labels/{1}.xml", MI_API_URL**)) { http.**TransportSettings**.Credentials = new NetworkCredential

Dispose of Injected HttpClient

北城以北 提交于 2019-12-21 02:44:11
问题 Our MVC application calls a WebAPI action using HttpClient. I decided to inject the HttpClient using StructureMap and override dispose in the controller public HomeController(HttpClient httpClient) { _httpClient = httpClient; } protected override void Dispose(bool disposing) { if (disposing && _httpClient != null) { _httpClient.Dispose(); } base.Dispose(disposing); } The StructureMap ObjectInitialize basically looks like this.. x.For<HttpClient>().Use(() => new HttpClient() { BaseAddress =

How to compress http request on the fly and without loading compressed buffer in memory

烈酒焚心 提交于 2019-12-20 12:35:11
问题 I need to send voluminous data in a http post request to a server supporting gziped encoded requests. Starting from a simple public async Task<string> DoPost(HttpContent content) { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync("http://myUri", content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } I've just added a pre compression public async Task<string> DoPost(HttpContent content, bool compress) { if

Using C# HttpClient to login on a website and scrape information from another page

时光毁灭记忆、已成空白 提交于 2019-12-20 11:36:13
问题 I am trying to use C# and Chrome Web Inspector to login on http://www.morningstar.com and retrieve some information on the page http://financials.morningstar.com/income-statement/is.html?t=BTDPF&region=usa&culture=en-US. I do not quite understand what is the mental process one must use to interpret the information from Web Inspector to simulate a login and simulate keeping the session and navigating to the next page to collect information. Can someone explain or point me to a resource ? For

HttpClient with asp.net WebApi in unit testing scenario

时光怂恿深爱的人放手 提交于 2019-12-20 10:59:58
问题 I have an integration test which I wanted to use as the basis of testing my WebAPI controllers with. Initially I thought I would have to set-up WebAPI in self-host mode and carry-out end-to-end tests over local Http. However I realised later by looking at the tests in the WebApiContrib project that its possible to set up an HttpClient with an HttpServer set-up with the correct service route to the WebAPI controller. I seems I can unit test the controllers without setting up WebApi in self

Google's OpenIDConnect return a Base64 token that cannot be parsed

怎甘沉沦 提交于 2019-12-20 04:37:22
问题 As exercise to understand OpenIDConnect, I am trying to authenticate in my web app with Google following this guide. The problem is I cannot read the token that Google sends to my application> var bytes = Convert.FromBase64String(codeEx.Id_token); var token = Encoding.ASCII.GetString(bytes); It fails in the first line saying: "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

HttpClient PostAsJsonAsync incompatible with Newtonsoft.Json

只愿长相守 提交于 2019-12-20 01:08:39
问题 Something I have just picked up in my winforms app My app does an http call to a web Api service as follows HttpClient _client = new HttpClient(); _client.Timeout = new TimeSpan(0, 3, 0); _client.BaseAddress = new Uri("http://Myserver/MyApp"); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = _client.PostAsJsonAsync("api/Addin", newObject).Result; Nothing fancy, but as soon as you install Newtonsoft.Json (V6.0.3)

WinRT C# - HttpRequestException: error 500

天大地大妈咪最大 提交于 2019-12-19 10:21:47
问题 I'm trying to port my WP8 application to a Windows Store App and I'm getting an HttpRequestException when performing the getStringAsync of the HttpClient. Here is the code snippet giving me the issue: HttpClient client = new HttpClient(); Stream stream = await client.GetStreamAsync(new Uri("http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=philippemexes", UriKind.Absolute)); and here is the complete exception message: HttpRequestException: Response status code does not indicate