FAQ Database Discussion Community


HttpClient not writing to stream while downloading

c#,http,stream,win-universal-app,dotnet-httpclient
Currently I am implementing a way to report Progress with the HttpClient, since we share code with a .NET4 WPF and a Windows Universal App we use the Microsoft HTTP Client Libraries from NuGet. The idea was to wrap the target file stream in a CountingInputStream and report progress there:...

HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding

c#,async-await,dotnet-httpclient,connection-timeout
We are using an HttpClient to post json to a restful web service. In one instance, we are running into something that has us baffled. Using tools like postman, fiddler etc, we can post to an endpoint and see that it is working. When we do the same with HttpClient.PostAsJsonAsync,...

HttpClient: Conditionally set AcceptEncoding compression at runtime

c#,gzip,.net-4.5,dotnet-httpclient,winrt-async
We are trying to implement user-determined (on a settings screen) optional gzip compression in our client which uses HttpClient, so we can log and compare performance across a number of different calls over a period of time. Our first attempt was to simply conditionally add the header as follows: HttpRequestMessage...

'system.argumentexception' When uploading multipart

c#,windows-phone,multipartform-data,dotnet-httpclient
I am trying to post multipart data using System.Net.Http.HttpClient but when I am instanciating my content I am getting this exception: A first chance exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL An exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL and wasn't handled before a managed/native boundary e: System.ArgumentException: The format...

Receive a PostAsync in FromBody as param

c#,asp.net-mvc-4,asp.net-web-api,dotnet-httpclient
I'm trying to read a JSON string from Web API controller that is send through a HttpClient.PostAsync() method. But for some reason the RequestBody is always null. My Request looks like this: public string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod) { var client = new HttpClient { BaseAddress = new...

Fetch XML via HttpClient and populate a List<>

c#,xml,rss,dotnet-httpclient
I am trying to fetch XML data from an RSS feed using the C# HttpClient and fill a List<RssFeedItem> with the data. This code is from another StackOverflow post: using (var httpClient = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com"); request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml"); var response...

Opening runspaces multiple times

c#,.net,powershell,c#-4.0,dotnet-httpclient
I want to execute a powershell command on a remote computer from C#. I have achieved the same using public Collection<PSObject> RunScript(String Command) { Collection<PSObject> results = null; using (var powershell = PowerShell.Create()) { Runspace runspace = RunspaceFactory.CreateRunspace(connection); runspace.Open(); powershell.Runspace = runspace(); powershell.AddScript(Command); results = powershell.Invoke(); runspace.Close(); } return results;...

REST service doesn't return a tuple correctly

c#,rest,tuples,dotnet-httpclient
I have REST service from which I want to return a tuple. The tuples will be 2 lists. See the code below. The GetRecords method returns the tuple and when I inspect the result variable I can see that everything that I require is there and correct. public HttpResponseMessage GetRecords([FromUri]...

async await for a HttpClient.PostAsync call

c#,asynchronous,async-await,dotnet-httpclient
I'm trying to wrap a call to PostAsync so I don't have to recode the call sequence all over my code base. The issue I'm having is the HttpResponseMessage I assigned from the call is not the same as the one the consumer of my method calls. Here is the...

Web API HttpClient PutAsync returning Http 404

rest,web-api,asp.net-web-api2,dotnet-httpclient
Im trying to send a PUT to my Web API and am struggling a bit as to how I should construct the actual Http request. Below is an integration test sample. It works fine using HttpMessageInvoker to call the Web API Put, but I want to use HttpClient in test...

How to preserve headers in the Flurl HttpClient

portable-class-library,dotnet-httpclient,flurl
I'm using the Furl.Http wrapper over the .NET Http Client. With each request my API requires a User-Agent and an Authorization header to be sent. I would like to set that up once, rather than having to state that every time. What I thought I would be able to do...

HttpClient.PutAsync finish immediately with no response

c#,async-await,.net-4.5,dotnet-httpclient
I try to upload a file with PUT method to the http server (Apache Tika) with the following code private static async Task<string> send(string fileName, string url) { using (var fileStream = File.OpenRead(fileName)) { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); var content = new StreamContent(fileStream); content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");...

HttpClient Instancing Per Service-Endpoint

c#,.net,asp.net-web-api,dotnet-httpclient
When instancing an HttpClient, the one common piece of advice is: Use a singleton, do not dispose after each use. However, based on this link I see commentary which I think implies another rule: The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is...

How to do XML POST with FlUrl

c#,c#-5.0,dotnet-httpclient,flurl
FlUrl does a great job in dealing with Json/UrlEncoded requests. However, the documentation doesn't really point out how to handle other request types such as text/xml. What's the best way to do an XML POST using FlUrl? This (accessing underlying HttpClient) kinda defeats the purpose of using FlUrl since you...

How should I reference HttpClient for dnx451 and dnxcore50?

c#,.net,asp.net-5,dotnet-httpclient,.net-core
How should I reference HttpClient using a project.json file? I want both frameworks to work: dnx451 and dnxcore50. Here is my current attempt at the project.json file. (I've removed the irrelevant parts.) { "dependencies": { "Microsoft.Net.Http": "2.2.29", "Microsoft.Net.Http.Headers": "1.0.0-beta4", "System.Net.Http": "4.0.0-beta-22816" }, "frameworks": { "dnx451": { "frameworkAssemblies": { "System.Net.Http": "4.0.0.0"...

Asp.net Web api2 impersonate on webClient call

c#,asp.net,webclient,dotnet-httpclient
we have a web api what has is how user account let's say ApplicationPoolUser that used have access to the databases used by the api, etc which work fine. but i'm trying to send a http get method on files on a remote server(sharepoint 2007) using webClient here's what im...

StringContent vs ObjectContent

c#,json,serialization,httpclient,dotnet-httpclient
I am using System.Net.Http's HttpClient to call a REST API with "POST" using the following code: using (HttpRequestMessage requestMessage = new HttpRequestMessage( HttpMethod.Post, new Uri(request)) { }) { response = await httpClient.PostAsync(request, objectContent); } The "objectContent" is currently this - objectContent = new ObjectContent(jsonContent.GetType(), jsonContent, new JsonMediaTypeFormatter()); I was wondering...

Task List with parameters

c#,.net,task-parallel-library,async-await,dotnet-httpclient
I'm needing to create a list of tasks to execute a routine that takes one parameter and then wait for those tasks to complete before continuing with the rest of the program code. Here is an example: List<Task> tasks = new List<Task>(); foreach (string URL in LIST_URL_COLLECTION) { tasks[i] =...

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

iis-7.5,asp.net-web-api2,dotnet-httpclient
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...

Upload an image via httpclient

c#,windows-phone,dotnet-httpclient
I have this code that uploads data public async Task<int> UploadAllDeposit() { tableSettings settings = App.ViewModelMaintenance.Setting; var q = from tableDeposit deposit in salesDB.Deposit where deposit.IsSync == false select deposit; int stat = 0; if (q.Count() > 0) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(App.ServiceURL);...

HttpClient - Xamarin Android - MvvmCross

android,xamarin,monodroid,mvvmcross,dotnet-httpclient
I've been developing an application for Android using Xamarin with MvvmCross and everything has been going fine, me and my team were able to develop the solution and this week we decided to make the final adjustments and to test the application in other environments. During the tests I had...

HttpClient IsComplete always return false

c#,asp.net,asp.net-mvc,asynchronous,dotnet-httpclient
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 =...