Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: HttpClient with POST parameters

I use codes below to send POST request to a server:

string url = "http://myserver/method?param1=1&param2=2"     HttpClientHandler handler = new HttpClientHandler(); HttpClient httpClient = new HttpClient(handler); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); HttpResponseMessage response = await httpClient.SendAsync(request); 

I don't have access to the server to debug but I want to know, is this request sent as POST or GET?

If it is GET, How can I change my code to send param1 & param2 as POST data (not in the URL)?

like image 690
Vahid Avatar asked Dec 09 '14 10:12

Vahid


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

A cleaner alternative would be to use a Dictionary to handle parameters. They are key-value pairs after all.

private static readonly HttpClient httpclient;  static MyClassName() {     // HttpClient is intended to be instantiated once and re-used throughout the life of an application.      // Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads.      // This will result in SocketException errors.     // https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.7.1     httpclient = new HttpClient();     }   var url = "http://myserver/method"; var parameters = new Dictionary<string, string> { { "param1", "1" }, { "param2", "2" } }; var encodedContent = new FormUrlEncodedContent (parameters);  var response = await httpclient.PostAsync (url, encodedContent).ConfigureAwait (false); if (response.StatusCode == HttpStatusCode.OK) {     // Do something with response. Example get content:     // var responseContent = await response.Content.ReadAsStringAsync ().ConfigureAwait (false); } 

Also dont forget to Dispose() httpclient, if you dont use the keyword using

As stated in the Remarks section of the HttpClient class in the Microsoft docs, HttpClient should be instantiated once and re-used.

Edit:

You may want to look into response.EnsureSuccessStatusCode(); instead of if (response.StatusCode == HttpStatusCode.OK).

You may want to keep your httpclient and dont Dispose() it. See: Do HttpClient and HttpClientHandler have to be disposed?

Edit:

Do not worry about using .ConfigureAwait(false) in .NET Core. For more details look at https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html

like image 106
aloisdg Avatar answered Sep 22 '22 02:09

aloisdg