Programmatically creating asp.net request and handling response

Recently one of the my friends asked about how to create a web request in asp.net to a url outside of project. So I decided to write a small blog post regarding this. This web request can be easily created with httpwebrequest class and you can easily consume the response we are getting from this. This kind of request can be very useful when you are implementing payment gateways or other third party components.  Following is a code for it. It is very easy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

namespace Experiment
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = "http://localhost:1798/WebForm2.aspx";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream);
            string response = streamReader.ReadToEnd();

            Response.Write(Response);
        }
    }
}

Here in the above code you can see I have created a webrequest with system.net name space and then I have typecast that request in HttpWebRequest Class. There are several parameters are available with request like method which HTML submit method you want to use like “Get” or “Post”.  After that I have created a object of HTTPResponse and then with the help of stream reader I have print a response we are getting from the URL.

That’s it. It’s very easy. Hope you will find it useful. Stay tuned for more. Happy Programming..

Technorati Tags: ,,,
Shout it
Published Monday, January 24, 2011 1:32 AM by Jalpesh P. Vadgama

Comments

# B-404241,B-404241.2, B&B Medical Services, Inc.; Rotech Healthcare, Inc., January 19, 2011 North Capitol Street

Pingback from  B-404241,B-404241.2, B&B Medical Services, Inc.; Rotech Healthcare, Inc., January 19, 2011 North Capitol Street

# re: Programmatically creating asp.net request and handling response

Tuesday, December 13, 2011 9:41 PM by Gracelyn

I tohught finding this would be so arduous but it's a breeze!

# re: Programmatically creating asp.net request and handling response

Friday, December 16, 2011 12:53 AM by Seston

That's relaly shrewd! Good to see the logic set out so well.

# re: Programmatically creating asp.net request and handling response

Saturday, June 02, 2012 7:27 PM by Sachin Karande

this coding but i want to create path of xml file which is give to client and i responds on those path

# re: Programmatically creating asp.net request and handling response

Tuesday, June 05, 2012 8:57 AM by Jalpesh P. Vadgama

@Sachin- You can create different content type for that. I will find the content type for that and will put here.