12 Steps to Make HTTP POST Web Request Using C# - Windows ASP.NET Core Hosting 2024 | Review and ComparisonWindows ASP.NET Core Hosting 2024 | Review and Comparison

This article will be very helpful for developers that want to know how to make HTTP POST web request using C#. Basically, like SMS sending application HTTP post web request is nothing but an internet transaction. But, for this purpose webserver connection must be on a true state.

How to Send Data Through HTTP POST WebRequest Class

In order to send data through HTTP POST WebRequest class, you need to go through the following step by step procedure. Usually, this procedure is used to submit data to a Web page.

1. Submit data to host server

Create a WebRequest application through calling WebRequest.Create with the URL (starting with ftp:, http:, https:, and file) of a resource like ASP.NET page that receives data.

If you need to set or read protocol-specific properties, you must cast your WebRequest or WebResponse object to a protocol-specific object type. Registration of create method is required through the static RegisterPrefix method.

_request.Credentials = CredentialCache.DefaultCredentials;

2. Set Property Values in Your WebRequest Object

The property of webRequest.Credentials need to be set for an instance in network credentials. Generally, the default value of credentials is null and you can put get and set method to fill a value for network credentials like password.

_request.Credentials = CredentialCache.DefaultCredentials;

3. Allow Data to be Transferred Using HTTP POST

_request.Method = "POST";

4. Assign ContentLength Property to the Number of Bytes

_request.ContentLength = byteArray.Length;

5. Make Sure ContentLength Property must be Set in Bytes

The content type property could be set to string for different MME contents like “text/html”, “image/gif”, “audio.wav”, and “application/pdf”.

_request.ContentType = "application/x-www-form-urlencoded";

6. Obtain the Stream that Carries Requested Information Through Calling the GetRequestStream Method

Stream _dataStream = request.GetRequestStream();

7. Writing the Data to the Stream Object through dataStream

_dataStream.Write(byteArray, 0, byteArray.Length);

8. Close the Request Stream Through the Stream.Close Method

_dataStream.Close();

9. Send the Request to the server through WebRequest.GetResponse Method

his method instantly returns an object carrying the response of the server. Also, the category of returned WebResponse object is identified by the scheme of the URI of request.

For instance, for accessing the HTTP-type properties of HttpWebResponse, transmit the WebResponse object to an HttpWebResponse orientation.

Console.WriteLine(((HttpWebResponse)response).StatusDescription);

10. Using request WebRequest.GetResponse to the Server

This method contains an object of the server’s response. WebResponse is associated with the scheme of the request of URI.

WebResponse _response = _request.GetResponse();

11. Call the WebResponse.GetResponseStream Method of the Object of WebResponse.

Stream _dataStream = _response.GetResponseStream();

12. Call WebResponse.Close method or Stream.Close Method to Close the Response Information.

_response.Close();

Conclusion

Hope your need of HTTP POST web request using C# is fulfilled now throughout the above steps. If any more information is required by you, please let me know.