In my opinion programming means thinking, and it is fun. My vision is to teach programming in more understandable manner to the students from rural background. I support programming for everyone

Posting data to URL in C#.NET

No comments
The following procedure describes the steps used to send data to a server. This procedure is commonly used to post data to a Web page.
requestResponse
public static string sendToServer(string Request)
        {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“URL”);
               
//HttpWebRequest class is used to Make a request to a Uniform Resource Identifier (URI). 
                request.ContentType = "application/x-www-form-urlencoded";
               // Set the ContentType property of the WebRequest.
                request.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(Request);
               // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length; 
               //Get the stream that holds request data by calling the GetRequestStream method.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                WebResponse response = request.GetResponse();
               // Get the stream containing content returned by the server.

                //Send the request to the server by calling GetResponse.
                dataStream = response.GetResponseStream();
              
// Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
              
// Read the content.
                string Response = reader.ReadToEnd();
                //return the response
                return Response;
        }



..Source MSDN

No comments :