Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i found this code to send http request.

C#
string url="http://www.contoso.com/default.html";
       string requestData = "";
       string RequestMethod = "POST";
       protected void Page_Load(object sender, EventArgs e)
       {
           try
           {
               /* Create a request using a URL that can receive a post*/
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
               /*set header*/
               request.Method = RequestMethod;
               //request.UserAgent="Mozilla/5.0";
               //request.Accept="";
               //request.Connection="";
               //request.ContentLength=requestData.Length;
               //request.ContentType="application/x-www-form-urlencoded";
               //request.Date="";
               //request.Expect="";
               //request.Host="";
               //request.Referer="";
               //request.TransferEncoding="";
               //request.Proxy=null;
               //request.Timeout="";
               //request.Credentials = CredentialCache.DefaultCredentials;
               //*********************
               Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData);
               /* Get the request stream.*/
               Stream requestStream = request.GetRequestStream();
               /*Write the data to the request stream*/
               requestStream.Write(postDataByte, 0, postDataByte.Length);
               /*Close the Stream object*/
               requestStream.Close();
               /*To send the request to the server, call GetResponse*/
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               /*Get Some Respone info*/
               Response.Write(((HttpWebResponse)response).StatusDescription);
               Response.Write(((HttpWebResponse)response).Headers);
               /*Get the stream containing content returned by the server*/
               Stream 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.*/
               Response.Write(responseFromServer);
               /*Clean up the streams*/
               reader.Close();
               dataStream.Close();
               response.Close();
           }
           catch (Exception)
           {
               Response.Write("An error occured");
           }
       }


the problem is i dont know why this part of code is necessary
C#
Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData);
                /* Get the request stream.*/
                Stream requestStream = request.GetRequestStream();
                /*Write the data to the request stream*/
                requestStream.Write(postDataByte, 0, postDataByte.Length);
                /*Close the Stream object*/
                requestStream.Close();


why i need to get request stream.
Posted

1 solution

Here is the link to the WebRequest.GetRequestStream Method [^] in MSDN. It is covered as:

Quote:
The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource.

The GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.


Also you should keep in mind that the post is getting triggered when the stream is closed. Getting the stream request does not triggers it.

Good luck,
OI
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900