dotnet-httpclient

How do I pass an object to HttpClient.PostAsync and serialize as a JSON body?

扶醉桌前 提交于 2019-12-17 06:33:08
问题 I'm using System.Net.Http , I found several examples on the web. I managed to create this code for make a POST request: public static string POST(string resource, string token) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(baseUri); client.DefaultRequestHeaders.Add("token", token); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("", "") }); var result = client.PostAsync("", content).Result; string resultContent = result.Content

Adding headers when using httpClient.GetAsync

為{幸葍}努か 提交于 2019-12-17 06:31:14
问题 I'm implementing an API made by other colleagues with Apiary.io, in a Windows Store app project. They show this example of a method I have to implement: var baseAddress = new Uri("https://private-a8014-xxxxxx.apiary-mock.com/"); using (var httpClient = new HttpClient{ BaseAddress = baseAddress }) { using (var response = await httpClient.GetAsync("user/list{?organizationId}")) { string responseData = await response.Content.ReadAsStringAsync(); } } In this and some other methods, I need to have

HttpClient - A task was cancelled?

若如初见. 提交于 2019-12-17 05:41:48
问题 It works fine when have one or two tasks however throws an error "A task was cancelled" when we have more than one task listed. List<Task> allTasks = new List<Task>(); allTasks.Add(....); allTasks.Add(....); Task.WaitAll(allTasks.ToArray(), configuration.CancellationToken); private static Task<T> HttpClientSendAsync<T>(string url, object data, HttpMethod method, string contentType, CancellationToken token) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url);

HttpClient - A task was cancelled?

送分小仙女□ 提交于 2019-12-17 05:40:09
问题 It works fine when have one or two tasks however throws an error "A task was cancelled" when we have more than one task listed. List<Task> allTasks = new List<Task>(); allTasks.Add(....); allTasks.Add(....); Task.WaitAll(allTasks.ToArray(), configuration.CancellationToken); private static Task<T> HttpClientSendAsync<T>(string url, object data, HttpMethod method, string contentType, CancellationToken token) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url);

Adding Http Headers to HttpClient

Deadly 提交于 2019-12-17 04:15:06
问题 All: I need to add http headers to the HttpClient before I send a request to a web service. How do I do that for an individual request (as opposed to on the HttpClient to all future requests)? I'm not sure if this is even possible. var client = new HttpClient(); var task = client.GetAsync("http://www.someURI.com") .ContinueWith((taskwithmsg) => { var response = taskwithmsg.Result; var jsonTask = response.Content.ReadAsAsync<JsonObject>(); jsonTask.Wait(); var jsonObject = jsonTask.Result; });

Why is HttpClient BaseAddress not working?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 02:27:07
问题 Consider the following code, where the BaseAddress defines a partial URI path. using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler)) { client.BaseAddress = new Uri("http://something.com/api"); var response = await client.GetAsync("/resource/7"); } I expect this to perform a GET request to http://something.com/api/resource/7 . But it doesn't. After some searching, I find this question and answer: HttpClient with BaseAddress. The suggestion is to place / on

How to handle recaptcha on third-party site in my client application

一曲冷凌霜 提交于 2019-12-13 20:02:38
问题 I was curious about how people build third-party apps for sites with NO public APIs , but I could not really find any tutorials on this topic. So I decided to just give it a try. I created a simple desktop application, which uses HttpClient to send GET requests to the site I frequently use, and then parses the response and displays the data in my WPF window. This approach worked pretty well (probably because the site is fairly simple). However, today I tried to run my application from a

UrlEncode non-string properties for HTTP Post through HttpClient

徘徊边缘 提交于 2019-12-13 16:15:03
问题 This code is in python dataParams = urllib.urlencode({ "name": "myname", "id": 2, }) dataReq = urllib2.Request('mylink', dataParams) dataRes = urllib2.urlopen(dataReq) Now i am trying to convert it into C#.Till now I have been able to do this only var dataParams = new FormUrlEncodedContent( new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("name", myname"), new KeyValuePair<string, string>("id", "2"), }); httpResponse = await httpClient.PostAsync(new Uri(dataString)

WebView display Loginscreen again after login via basic authentication with HttpClient

旧巷老猫 提交于 2019-12-13 08:17:59
问题 i am connecting to a server with basic authentication and after that i am calling the URL in Webview with following code: WebView.Source(new Uri("https:(//UrlHere")); The Webview Puts up a Login-Window, but i am already logged in. Why is this happening? How can i prevent this? The way i a authenticate to the server: private async void HttpClientCall(object sender, RoutedEventArgs e) { System.Diagnostics.Debug.WriteLine(this.GetType().Name + ": HTTPCLientCall entered"); //System.Diagnostics

IIS serves a compressed (gzip) response to Chrome Postman but not to .NET HttpClient

岁酱吖の 提交于 2019-12-13 05:25:35
问题 I have created a REST web service using Web API 2.2 on a Windows Server 2008 R2 box running IIS 7.5. The problem that I'm having is that the web service is returning a compressed response (Content-Encoding: gzip) when I make the request through the Google Chrome Postman application. But when I make the same request using the .NET 4.5.1 HttpClient, the server does not return a compressed response (the Content-Encoding header is blank). Here is my C# code: var handler = new HttpClientHandler();