Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What purposes should I use class StringContent for?

Tags:

c#

asp.net-mvc

There is StringContent class in System.Net.Http namespace. What purposes should I use class StringContent for?

like image 777
mtkachenko Avatar asked Oct 20 '13 15:10

mtkachenko


People also ask

How do I use Postasync client?

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 Formurlencodedcontent C#?

A container for name/value tuples encoded using application/x-www-form-urlencoded MIME type.

What is HttpContent?

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


1 Answers

StringContent class creates a formatted text appropriate for the http server/client communication. After a client request, a server will respond with a HttpResponseMessageand that response will need a content, that can be created with the StringContent class.

Example:

 string csv = "content here";  var response = new HttpResponseMessage();  response.Content = new StringContent(csv, Encoding.UTF8, "text/csv");  response.Content.Headers.Add("Content-Disposition",                                "attachment;                                filename=yourname.csv");  return response; 

In this example, the server will respond with the content present on the csv variable.

like image 89
Lombas Avatar answered Sep 19 '22 23:09

Lombas