Questions tagged [httpcontent]

79 questions
356
votes
8 answers

Where is HttpContent.ReadAsAsync?

I see in tons of examples on the web using the new HttpClient object (as part of the new Web API) that there should be HttpContent.ReadAsAsync method. However, MSDN doesn't mention this method, nor does IntelliSense find it. Where did it go, and…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
355
votes
3 answers

How do I set up HttpContent for my HttpClient PostAsync second parameter?

public static async Task GetData(string url, string data) { UriBuilder fullUri = new UriBuilder(url); if (!string.IsNullOrEmpty(data)) fullUri.Query = data; HttpClient client = new HttpClient(); HttpResponseMessage…
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
89
votes
3 answers

Read HttpContent in WebApi controller

How can I read the contents on the PUT request in MVC webApi controller action. [HttpPut] public HttpResponseMessage Put(int accountId, Contact contact) { var httpContent = Request.Content; var asyncContent =…
Marty
  • 3,485
  • 8
  • 38
  • 69
71
votes
13 answers

Retrying HttpClient Unsuccessful Requests

I am building a function that given an HttpContent Object, will issues request and retry on failure. However I get exceptions saying that HttpContent Object is disposed after issuing the request. Is there anyway to copy or duplicate the…
samirahmed
  • 1,219
  • 2
  • 12
  • 16
38
votes
3 answers

How to set large string inside HttpContent when using HttpClient?

So, I created a HttpClient and am posting data using HttpClient.PostAsync(). I set the HttpContent using HttpContent content = new FormUrlEncodedContent(post_parameters); where post_parameters is a list of Key value pairs List
Muraad
  • 1,110
  • 1
  • 9
  • 14
34
votes
2 answers

HTTPError Exception Message not displaying when webapi is run on Server vs being run locally

I have a webapi that runs on an IIS7.5 server. It has 3 controllers, and all 3 can be used to access the webapi from calls within my application. I had a error where my base class for my controller was exposing it's functions as public, rather than…
cookiekitty
  • 984
  • 1
  • 8
  • 11
21
votes
3 answers

HttpContent boundary double quotes

I have this code sample that was posted as an answer to another question (Send a file via HTTP POST with C#). It works fine except for one issue. It surrounds the boundary in the HTTP header with double quotes: multipart/form-data;…
jtaylor___
  • 609
  • 1
  • 6
  • 14
12
votes
1 answer

Post JSON HttpContent to ASP.NET Web API

I have an ASP.NET Web API hosted and can access http get requests just fine, I now need to pass a couple of parameters to a PostAsync request like so: var param = Newtonsoft.Json.JsonConvert.SerializeObject(new { id=_id, code = _code }); HttpContent…
Leigh
  • 1,495
  • 2
  • 20
  • 42
11
votes
2 answers

HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours)

We are building a highly concurrent web application, and recently we have started using asynchronous programming extensively (using TPL and async/await). We have a distributed environment, in which apps communicate with each other through REST APIs…
alextercete
  • 4,871
  • 3
  • 22
  • 36
10
votes
2 answers

HttpContent Headers inconsistent enumeration

I am transforming HttpContent into the following dto: public class ContentDto { public string ContentType {get; set;} public string Headers {get; set; } public object Data { get; set; } public ContentDto(HttpContent content) …
Luiso
  • 4,173
  • 2
  • 37
  • 60
9
votes
1 answer

Mocking HttpMessageHandler with moq - How do I get the contents of the request?

Is there a way to get the contents of the http request before deciding what kind of response I want to send back for the test? Multiple tests will use this class and each test will have multiple http requests. This code does not compile because the…
rjacobsen0
  • 1,287
  • 13
  • 25
9
votes
3 answers

Set Authorization Header of HttpClient

I have the following code, and I want to set the Authorization of the post request to be like this: Authorization:key=somevalue using (HttpClient client = new HttpClient()) { using (StringContent jsonContent = new StringContent(json)) { …
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
8
votes
2 answers

How do I send arbitrary JSON data, with a custom header, to a REST server?

TL;DR -- How do I send a JSON string to a REST host with an auth header? I've tried 3 different approaches found one that works with anonymous types. Why can't I use anonymous types? I need to set a variable called "Group-Name", and a hyphen…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
6
votes
2 answers

Extract content data from HttpCllient's FormUrlEncodedContent

My content is: var content = new Dictionary { {"pickup_date", pickupDate.ToString("dd.MM.yyyy HH:mm")}, {"to_city", "Victoria"}, {"delivery_company", "4"}, {"shop_refnum", parameters.Reference}, …
FireShock
  • 1,082
  • 1
  • 15
  • 25
5
votes
3 answers

Stream from response.Content.ReadAsStreamAsync() is not readable randomly

I am making a .Net Web API application in which the following code is making call to my different c# application to download file and then save it on the disk. Sometimes everything works fine and I get the file but sometimes the below code is not…
1
2 3 4 5 6