dotnet-httpclient

How to know from which point HttpClient task cancellation is triggered

别等时光非礼了梦想. 提交于 2019-12-11 19:52:10
问题 I have the following code to fetch the bearer token and then call the Web API in a c# application. public async Task<HttpResponseMessage> SendHttpRequest() { HttpResponseMessage response = null; try { HttpClient client = new HttpClient(); string accessToken = await GetBearerToken(resourceUrl, clientId, clientSecret, tokenProviderUrl); if (!string.IsNullOrEmpty(accessToken)) httpRequest.Headers.Add("Authorization", ("Bearer " + accessToken)); response = await client.SendAsync(httpRequest); }

Why return 500 error while calling another controller using WebClient?

核能气质少年 提交于 2019-12-11 19:47:57
问题 I'm testing example on this link: http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetmvc4_topic5#_Toc319061802 but I have 500 error calling another controller using WebClient. When I access to "http://localhost:2323/photo/gallery directly is running, but I'm trying from action using WebClient it return 500 error? Why?" public ActionResult Index() { WebClient client = new WebClient(); var response = client.DownloadString(Url.Action("gallery", "photo", null, Request.Url.Scheme)); var jss

HttpClient inside PCL throws NotImplementedException when called from a Silverlight app

拜拜、爱过 提交于 2019-12-11 18:15:57
问题 I have a portable class library (profile 147 supporting .NET4.03, Silverlight 5, WinPhone 8 and WinStore) that uses HttpClient to make HTTP calls. The library works fine in all platforms except for Silverlight 5. When the library is used from an SL5 application, an attempt to call SendAsync method with GET verb results in NotImplementedException: {System.NotImplementedException: The method or operation is not implemented. at System.Net.Browser.ClientHttpWebRequest.set_AllowAutoRedirect

Cancellation not returning past whenall using httpclient

匆匆过客 提交于 2019-12-11 16:13:59
问题 Here is an updated version of the code posted under httpclient.GetStringAsync blocking The question is when cancel is done, though the tasks are cancelled, I am expecting Await Task.WhenAll(tasks) to return and print whats in finally , but its not. I can see task cancellations when pressing cancel and I also see the connections reduce to 0, but the finally guessing WhenAll still thinks some tasks are being executed. Here is the code: Private concurrencySemaphore As New SemaphoreSlim(10)

Xamarin Form HttpClient Stuck

跟風遠走 提交于 2019-12-11 13:43:23
问题 I'm trying to get response from soundcloud API. Here is my code. public static async Task<string> GetTheGoodStuff() { var client = new HttpClient(new NativeMessageHandler()); var response = await client.GetAsync("http://api.soundcloud.com/playlists?client_id=17ecae4040e171a5cf25dd0f1ee47f7e&limit=1"); var responseString = response.Content.ReadAsStringAsync().Result; return responseString; } But it's stucks on var response = await client.GetAsync . How can I fix this? Thanks! 回答1: I did just

Async call with HttpClient in PCL

…衆ロ難τιáo~ 提交于 2019-12-11 13:33:40
问题 I have a PCl in which I want to make a async call usingg HttpClient. I coded like this public static async Task<string> GetRequest(string url) { var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue }; HttpResponseMessage response = await httpClient.GetAsync(url); return response.Content.ReadAsStringAsync().Result; } But await is showing error "cannot await System.net.http.httpresponsemessage" like message. If I use code like this than everything goes well but not in

Make HTTP Request from C# HttpClient to Same Machine using IP Address

末鹿安然 提交于 2019-12-11 12:56:45
问题 Basically, I need to be able to make an HTTP Request to a Website on the same machine I am on, without modifying the host file to create a pointer to the domain name. For example. I am running the code on one website, let's say www.bobsoft.com which is on a server. I need to make an HTTP request to www.tedsoft.com which is on the same server. How can I make a call using a C# HttpClient without modifying the host file? Take into account that the websites are routed by bindings in IIS. I do

DNS resolution failure in HttpClient throws Unhandled AggregateException

浪子不回头ぞ 提交于 2019-12-11 12:41:02
问题 I have a .NET 4 client app using HttpClient. When the _tokenServiceUrl does not correctly resolve via DNS (for example: a network change change occurs when a VPN goes up or down or when mobile devices lose connectivity in a dead spot) then an AggregateException gets thrown. I'm trying to catch that exception, but can't. try { var response = _client.GetAsync(_tokenServiceUrl).Result; response.EnsureSuccessStatusCode(); var token = response.Content.ReadAsAsync<string>().Result; return token; }

Applying default headers to multi-part request content

こ雲淡風輕ζ 提交于 2019-12-11 12:06:24
问题 HttpClient has a convenience property DefaultRequestHeaders which defines default headers to be included in any request. I'm trying to apply this to the batching support in Web API, but I've noticed that these default headers are not applied to multi-part HttpMessageContent requests: var client = new HttpClient(); client.DefaultRequestHeaders.Add("X-Some-Header, "foobar"); var batchContent = new MultipartContent("mixed"); foreach(var x in something) { // these inner requests don't get default

How many connections in HttpClient

雨燕双飞 提交于 2019-12-11 11:35:00
问题 Background I have to download about 16k documents and the same amount of html pages from the internet. This number will increase in the future. Currently I am just using Parallel.ForEach to download and work on the data in parallel. This however does not seem to fully utilize my resources, so I am planning to bring async/await into play, to have as many downloads running in asynchronously as possible, but I will probably have to limit that. Actual Question How many open connections can a