"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, June 26, 2011

http-request and -responce handling in server side

On previous post Handling httprequest using jquery. Today Handling that on server side.
Code
Invoke 2 namespace 
  • using System.Net; 
  • using System.IO;
On Page1.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "http://localhost:5136/httprequest/Default2.aspx?name='"+"data"+"'";
        HttpWebRequest htprqust = (HttpWebRequest)WebRequest.Create(url);
        htprqust.Method = "POST";
        htprqust.ContentType = "text";
       // htprqust.ContentType = "";

        HttpWebResponse htprspn = (HttpWebResponse)htprqust.GetResponse();
        Stream responceStream = htprspn.GetResponseStream();
        StreamReader sreader = new StreamReader(responceStream);
        string _respoce = sreader.ReadToEnd();
        Response.Write(_respoce);

    }

On Page2.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["name"] == null)
            Response.Write("n");
        else
        {
            string _Name = Request.QueryString["name"].ToString();
            if (_Name == "data")
                Response.Write("s");
            else
                Response.Write("n");
        }
    }
 
Download sample from here 

 Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: