Introduction
The developers, who are keen to know how to make HTTP POST web request using C #, will have all the needful information definitely from this article. 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.HTTP Status Codes: very important to know with Web Request
Procedures 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.Step #1:
To submit data to a 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.
WebRequest _request = WebRequest.Create("https://www.technologycrowds.com");
Step #2:
Then you need to set any property values that you require 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.WebRequest _request = WebRequest.Create("https://www.technologycrowds.com");
_request.Credentials = CredentialCache.DefaultCredentials;
Step #3:
Mention a protocol method like HTTP POST method that allows data to be transferred with a request._request.Credentials = CredentialCache.DefaultCredentials;
_request.Method = "POST";
Step #4"
_request.Method = "POST";
You can assign ContentLength property to the number of bytes can include with your request.
_request.ContentLength = byteArray.Length;
Step #5:
The content length property must be set in bytes (byteArray) and then the content types property needs to be set with an appropriate value such as "application/x-www-form-urlencoded". Sometimes length of bytes may be increased or decreased as per application requirement. For example: Int32, Int16, Int64. _request.ContentLength = byteArray.Length;
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";
Step #6:
Now, obtain the stream that carries requested information through calling the GetRequestStream method. Generally, in this method stream is used to write the information to internet sources._request.ContentType = "application/x-www-form-urlencoded";
Stream _dataStream = request.GetRequestStream();
Step #7:
Write the required information by writing the data to the Stream object through dataStream along with GetRequestStream method._dataStream.Write(byteArray, 0, byteArray.Length);
Step #8:
Now, close the request stream through the Stream.Close method._dataStream.Close();
Step #9:
Now, it’s time to send the request to the server through WebRequest.GetResponse method. This 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);
Step #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();
Step #11:
To receive the stream carrying response information sent by the server, you need to call the WebResponse.GetResponseStream method of the object of WebResponse.Stream _dataStream = _response.GetResponseStream();
Step #12:
After reading the response information, you need to call WebResponse.Close method or Stream.Close method to close the response information._response.Close();
using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestPostExample { public static void Main() { // Creating a web request using a URL that can response a post. WebRequest _request = WebRequest.Create("https://www.technologycrowds.com"); // Set the Method property of the request to POST. _request.Method = "POST"; // Create a POST with data and convert back it to a bytes array. string _postData = "It is a test post to a Web server."; byte[] byteArray = Encoding.UTF8.GetBytes(_postData); // Set the ContentType property of the WebRequest. _request.ContentType = "application/x-www-form-urlencoded"; // here setting the ContentLength property of the WebRequest. _request.ContentLength = byteArray.Length; // here gettting the request stream. Stream _dataStream = _request.GetRequestStream(); // Writing the data to the request stream. _dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. _dataStream.Close(); // Get the response. WebResponse _response = _request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)_response).StatusDescription); // Get the stream containing content returned by the server. // The using block ensures the stream is automatically closed. using (dataStream = _response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); } // Close the response. _response.Close(); } } }
Post A Comment:
0 comments: