dotnet-httpclient

System.Net.Http.HttpClient adding If-Modified-Since header

流过昼夜 提交于 2019-12-11 09:52:25
问题 I am attempting to issue an Http Get request from a windows universal app and seeing an odd behavior. (not sure if the fact that it is a universal app is related or not). The simplified code in question is this: var client = new HttpClient(); var response = await client.GetAsync("https://storage.googleapis.com/pictureframe/settings.json"); var s = await response.Content.ReadAsStringAsync(); In a unit test or console app that works as expected and the variable s contains the json content.

httpclient.GetStringAsync blocking

冷暖自知 提交于 2019-12-11 08:24:17
问题 I must be doing something wrong since this code is blocking and runs synchronously, inspite of calling the async method of GetStringAsync . Any help would really help me understand the reasons: Private Async Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click Dim urls As List(Of String) = SetUpURLList() Dim baseAddress = New Uri("http://www.amazon.com") ServicePointManager.DefaultConnectionLimit = 10 Dim requestNumber As Integer = 0 For Each url In urls requestNumber +=

HttpClient not throwing after lost connection

主宰稳场 提交于 2019-12-11 08:08:43
问题 I have a Windows Store application in which I need to download a file a couple of megabytes in size. I am trying to do this using the HttpClient. Here is a simplification of the code: using (var httpClient = new HttpClient()) { var request = await httpClient.SendAsync( new HttpRequestMessage( HttpMethod.Get, "http://openpandora.info:8080/Battlefield%204%20-%20Fishing%20in%20Baku%20-%20Xbox%20One.mp4"), HttpCompletionOption.ResponseHeadersRead); var outputFile = await ApplicationData.Current

HttpClient IsComplete always return false

孤街浪徒 提交于 2019-12-11 06:28:20
问题 I'm trying to authorize user for get data from remote xml web service by using HttpClient GetAsync method. Unfortunately regardless server answer result.IsCompleted alaways return false in Controller. What I'm doing wrong? This is Controller: [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(CredentialsViewModel model) { if (!ModelState.IsValid) return View("Login"); var result = ar.AuthenticateUser(model.UserName, model.Password); if (!result.IsCompleted) {

Digest authentication in Windows Store app using HttpClient (C#)

∥☆過路亽.° 提交于 2019-12-11 05:53:38
问题 I'm struggling with this problem for a week. I have to use API with Digest authentication in Windows Store App, but while I'm using this code I get System.ArgumentNullException in this line of code: HttpHandler.Credentials = credCache; Here is rest of code: var credCache = new CredentialCache(); credCache.Add(new Uri("https://myserverIP/api"),"Digest",new NetworkCredential("mylogin", "mypassword") ); var HttpHandler = new HttpClientHandler(); HttpHandler.Credentials = credCache; var

How to get response body from httpclient c# when doing multipart

痴心易碎 提交于 2019-12-11 05:36:55
问题 I am trying to post multipart data using System.Net.Http.HttpClient, the obtained response is 200 ok. Here is the Method I used: public async Task postMultipart() { var client = new HttpClient(); client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data"); // This is the postdata MultipartFormDataContent content = new MultipartFormDataContent( ); content.Add(new StringContent("12", Encoding.UTF8), "userId"); content.Add(new StringContent("78", Encoding.UTF8),

HttpClient missing response headers in WinRT / Win8

浪子不回头ぞ 提交于 2019-12-11 03:58:29
问题 I'm using HttpClient in a Windows 8 app and it seems that it purposely hides custom headers in the response. For example: Our response received has a custom header called "Sample-Header: 123" I expect that the headers in the response content would contain "Sample-Header" with a value of "123" var client = new HttpClient(); var response = await client.GetAsync(uri); string sample; IEnumerable<string> values; if (response.Content.Headers.TryGetValues("Sample-Header", out values)) { // This

Async/Await calls using httpClient with timeout

我们两清 提交于 2019-12-11 01:52:05
问题 Based on the answer of another question: HttpClient async requests not completing for large batch sent out in a loop I'm using the extension method and applying the logic they propose to use timeout with HttpClient class and avoid hangs or no response from it. public static Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout) { var delay = task.ContinueWith(t => t.Result , new CancellationTokenSource(timeout).Token); return Task.WhenAny(task, delay).Unwrap(); } So, calling HttpClient

Does HttpHeaders.TryAddWithoutValidation validate or not?

点点圈 提交于 2019-12-10 18:26:38
问题 I'm a bit confused by the name of this function. Why isn't it just TryAdd ? What is it that it doesn't validate? If I use this function, can it still throw in some cases? Can I somehow "try to add" without getting any exceptions and only false returned if it fails? EDIT: I think the 2 questions are opposing each other. If I call with TryAddWithoutValidation does it actually add the invalid headers or it just returns false if they are invalid? 回答1: The HttpHeaders.TryAddWithoutValidation

C# httpClient (block for async call) deadlock

人走茶凉 提交于 2019-12-10 18:19:20
问题 Current Situation There is a client that does a get-request by HttpClient.GetAsync. Unfortunately, for some reason, we need to block on that calls. In order to do so, this Asynchelper class is used in order to avoid context-switch deadlocks (instead of just using .Result) public static class AsyncHelper { private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); public static