There is StringContent class in System.Net.Http namespace. What purposes should I use class StringContent for?
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.
A container for name/value tuples encoded using application/x-www-form-urlencoded MIME type.
A base class representing an HTTP entity body and content headers.
StringContent class creates a formatted text appropriate for the http server/client communication. After a client request, a server will respond with a HttpResponseMessage
and 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With