Wednesday, January 5, 2011

send c# Web request (http/https)

Hey folks..!!!
this is something i wanted to do while developing a web application.. i just wanted post some data to verify my site working accordingly.
Hope this one will be helpful.
                WebRequest request = WebRequest.Create("your site address"));
                request.Method = "POST";
       //Cast if it is https simply for the secured connections
               // ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;
                string postData = "yout data";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                WebResponse response = request.GetResponse();
              // get the response
                dataStream.Close();
                response.Close();
            }

P.S.
remenber to add 


using System.IO;
using System.Net;

No comments:

Post a Comment