Wednesday, May 28, 2014

How to send and Received HTTP Request/Response

Today we will talk about HTTP request, how to send and receive http request. We can use WebClient and HttpwebRequest to send the Request, but there are some advantages of HttpWebRequest over WebClient.
We mostly used the webclient for uploading and downloading the files, but with HttpwebRequest we can do a lot. We can set it request timeout and content Type that would be handled on service side.
Please comment if like this post, so that i continued posting.

Here is code for client side. you can use it with window form.

private void btnSend_Click(object sender, EventArgs e)
            // Create new Datatable
            DataTable dtEmployee = new DataTable("Employee");

            // Add Columns
            DataColumn DColID = new DataColumn("Emp_ID", System.Type.GetType("System.Int32"));

            DataColumn DColName = new DataColumn("Name", System.Type.GetType("System.String"));

            DataColumn DColDepart = new DataColumn("Department", System.Type.GetType("System.String"));

            //Adding data
            dtEmployee.Rows.Add("1201", "Wasi", "IT");
            dtEmployee.Rows.Add("1301", "Shref", "HR");

            DataSet dsRequest = new DataSet();

            // Now create an Object of System.IO.MemoryStream
            MemoryStream ms = new MemoryStream();
            // Write dataset into memorystream
            dsRequest.WriteXml(ms, XmlWriteMode.WriteSchema);

            //string URL = @"http://localhost/TestProject/Test.aspx";

            string URL = @"http://localhost:59580/Default.aspx";
            //Create an object of HTTPWebRequest and pass it URL
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(URL);

            //convert memory stream into byte to assign it to httpwebrequest
            Byte[] ReqByte = ms.ToArray();

            //Now you can take advantages of using HTTPWebRequest over WebClient. you can set its different properties
            httpReq.ContentLength = ReqByte.Length;
            httpReq.Timeout = 10000;
            httpReq.ContentType = "xml";
            httpReq.Method = "POST";
            httpReq.KeepAlive = false;

            //Create an object of Stream and write the byte into it.
            Stream ReqStream = httpReq.GetRequestStream();
            ReqStream.Write(ReqByte, 0, ReqByte.Length);

            //Create an object of HttpWebResponse to Read the Response
            HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

            // Assign Response Stream to Stream Reader
            StreamReader RespStreamReader = new StreamReader(httpResp.GetResponseStream());
            string strResponse = RespStreamReader.ReadToEnd();

Code that will be used in page load event of an aspx page (Web Project).
protected void Page_Load(object sender, EventArgs e)
            //Validate the contenttype that you send from client side
            if (Request.ContentType.ToString().Equals("xml"))
                //Create an object of streamreader and read the page request inputstream
                StreamReader ReqStrmReader = new StreamReader(Page.Request.InputStream);
                string strRequest = ReqStrmReader.ReadToEnd();

                if (!string.IsNullOrEmpty(strRequest))
                    //Convert the Reqeust string into byte
                    Byte[] btReq = System.Text.Encoding.ASCII.GetBytes(strRequest);

                    //Create an object of Memory Stream and assign these byte to it.
                    MemoryStream ms = new MemoryStream(btReq);

                    //Create a dataset and assign this stream to it
                    DataSet dsReq = new DataSet();

                    Response.Write("Dataset Received");

                    // that is the end. now you can use this dataset anywhere else.

Search This Blog