How to Submit Http Form Using C#

How to submit http form using C# in DOTNET CORE

Dotnet core support

var s = "some html / html form";

HttpContext.Response.WriteAsync(s.ToString());

Find refrence :
https://gist.github.com/priore/7163408

Is there an equivalent to "HttpContext.Response.Write" in Asp.Net Core 2?

Submit form using HttpClient in C#

HttpClient.PostAsync return a Task<HttpResponseMessage> so normally it would need to be awaited. As you are using it in main method you would have to get the result from the task

var response = client.PostAsync(uri, formContent).GetAwaiter().GetResult();

Or the simpler

var response = client.PostAsync(uri, formContent).Result;

In both cases response would be an instance of HttpResponseMessage. You can inspect that instance for HTTP status and the content of the response.

If using .net core you can even use an async Main method like

static async Task Main(string[] args) {

//..code removed for brevity

var response = await client.PostAsync(uri, formContent);
var content = await response.Content.ReadAsStringAsync();
//...
}


Related Topics



Leave a reply



Submit