make a http request with a custom body as html in csharp

To make an HTTP request with a custom body as HTML in C#, you can use the HttpClient class along with the HttpContent class. Here's an example:

main.cs
using System.Net.Http;
using System.Text;

class Program
{
    static async Task Main(string[] args)
    {
        using (var client = new HttpClient())
        {
            var content = new StringContent("<html><body><h1>Hello, World!</h1></body></html>", Encoding.UTF8, "text/html");
            var response = await client.PostAsync("http://example.com", content);
            var responseContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseContent);
        }
    }
}
514 chars
17 lines

In this example, we create a new StringContent object with our HTML body, set the content type to text/html, and then make a POST request to http://example.com with the content. We then read the response and print it to the console.

Note that you will need to add a using statement for System.Net.Http at the top of your file in order to use the HttpClient class.

related categories

gistlibby LogSnag