Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

public static async Task<string> GetData(string url, string data) {     UriBuilder fullUri = new UriBuilder(url);      if (!string.IsNullOrEmpty(data))         fullUri.Query = data;      HttpClient client = new HttpClient();      HttpResponseMessage response = await client.PostAsync(new Uri(url), /*expects HttpContent*/);      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");     response.EnsureSuccessStatusCode();     string responseBody = await response.Content.ReadAsStringAsync();      return responseBody; } 

The PostAsync takes another parameter that needs to be HttpContent.

How do I set up an HttpContent? There Is no documentation anywhere that works for Windows Phone 8.

If I do GetAsync, it works great! but it needs to be POST with the content of key="bla", something="yay"

//EDIT

Thanks so much for the answer... This works well, but still a few unsures here:

    public static async Task<string> GetData(string url, string data)     {         data = "test=something";          HttpClient client = new HttpClient();         StringContent queryString = new StringContent(data);          HttpResponseMessage response = await client.PostAsync(new Uri(url), queryString );          //response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");         response.EnsureSuccessStatusCode();         string responseBody = await response.Content.ReadAsStringAsync();          return responseBody;     } 

The data "test=something" I assumed would pick up on the api side as post data "test", evidently it does not. On another matter, I may need to post entire objects/arrays through post data, so I assume json will be best to do so. Any thoughts on how I get post data through?

Perhaps something like:

class SomeSubData {     public string line1 { get; set; }     public string line2 { get; set; } }  class PostData {     public string test { get; set; }     public SomeSubData lines { get; set; } }  PostData data = new PostData {      test = "something",     lines = new SomeSubData {         line1 = "a line",         line2 = "a second line"     } } StringContent queryString = new StringContent(data); // But obviously that won't work 
like image 816
Jimmyt1988 Avatar asked Sep 24 '13 01:09

Jimmyt1988


People also ask

What is HttpContent?

A base class representing an HTTP entity body and content headers.

How do I use HttpClient Getasync?

Using SendAsync, we can write the code as: static async Task SendURI(Uri u, HttpContent c) { var response = string. Empty; using (var client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage { Method = HttpMethod. Post, RequestUri = u, Content = c }; HttpResponseMessage result = await client.

What is HttpClient PostAsync?

PostAsync(String, HttpContent) Send a POST request to the specified Uri as an asynchronous operation. PostAsync(Uri, HttpContent) Send a POST request to the specified Uri as an asynchronous operation.

What is HttpClient SendAsync?

SendAsync(HttpRequestMessage) Send an HTTP request as an asynchronous operation. SendAsync(HttpRequestMessage, HttpCompletionOption) Send an HTTP request as an asynchronous operation.


2 Answers

This is answered in some of the answers to Can't find how to use HttpContent as well as in this blog post.

In summary, you can't directly set up an instance of HttpContent because it is an abstract class. You need to use one the classes derived from it depending on your need. Most likely StringContent, which lets you set the string value of the response, the encoding, and the media type in the constructor. See: http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

like image 156
Preston Guillot Avatar answered Oct 09 '22 13:10

Preston Guillot


To add to Preston's answer, here's the complete list of the HttpContent derived classes available in the standard library:

Credit: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

Credit: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

There's also a supposed ObjectContent but I was unable to find it in ASP.NET Core.

Of course, you could skip the whole HttpContent thing all together with Microsoft.AspNet.WebApi.Client extensions (you'll have to do an import to get it to work in ASP.NET Core for now: https://github.com/aspnet/Home/issues/1558) and then you can do things like:

var response = await client.PostAsJsonAsync("AddNewArticle", new Article {     Title = "New Article Title",     Body = "New Article Body" }); 
like image 39
Serj Sagan Avatar answered Oct 09 '22 11:10

Serj Sagan