Click here to Skip to main content
11,411,840 members (49,109 online)
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
mit62804

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)

  Print Answers RSS
0 OriginalGriff 243
1 Sergey Alexandrovich Kryukov 200
2 Sascha Lefévre 130
3 ProgramFOX 130
4 Maciej Los 90
0 Sergey Alexandrovich Kryukov 8,955
1 OriginalGriff 7,158
2 Maciej Los 3,480
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web02 | 2.8.150414.5 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2015
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