Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
hi i found this code to send http request.
 
 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
 
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 24-Apr-13 22:42pm
mit62773

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100