Thursday, February 4, 2010

Send HTTP Web Request using C# Application


In some application, we need to send HTTP web request to web application server. While sending http request to server our .Net code block the execution till the response come back to us. But in some scenario we need not wait for response on the same time because we need that output later on.


For the above problem, we can send asynchronous HTTP web request to web application server. When we send web request asynchronous then our application resume the execution and when we need the output of that web request then we can get is using IAsyncResult object.

Here I explained come sample to send asynchronous HTTP web request to web application.

Before starting it, we need to understand the web request types. Actually, any browser send HTTP request using two methods:

  • GET
  • POST
In GET method, Browser sends form data into query string, so user can easily view the data in address bar and also change the data.

In POST method, Browser sends form data with URL as an attachment. User can’t see the POST data in address bar so it is not possible to change the data from address bar.

First we go through asynchronous HTTP web request method using “GET”. Please check the code below:

///
/// Gets the response.
///

/// <param name="StrURL">The URL.
/// HTML source
public string GetResponse(string StrURL)
{
string strReturn = "";
HttpWebRequest objRequest = null;
IAsyncResult ar = null;
HttpWebResponse objResponse = null;
StreamReader objs = null;
try
{

objRequest = (HttpWebRequest)WebRequest.Create(StrURL);
ar = objRequest.BeginGetResponse(new AsyncCallback(GetScrapingResponse), objRequest);

//// Wait for request to complete
ar.AsyncWaitHandle.WaitOne(1000 * 60, true);
if (objRequest.HaveResponse == false)
{
throw new Exception("No Response!!!");
}

objResponse = (HttpWebResponse)objRequest.EndGetResponse(ar);
objs = new StreamReader(objResponse.GetResponseStream());
strReturn = objs.ReadToEnd();
}
catch (Exception exp)
{
throw exp;
}
finally
{
if (objResponse != null)
objResponse.Close();
objRequest = null;
ar = null;
objResponse = null;
objs = null;
}
return strReturn;
}

///
/// Gets the scraping response.
///

/// <param name="result">The result.
protected void GetScrapingResponse(IAsyncResult result)
{

}


Let’s understand the above code:

We have created the method “GetResponse” which take URL as a parameter. You have to pass query string data with URL in this method.

For example: you can call this method like

GetResponse(“http://www.codeproject.com/?cat=4”);


In this method, I created object for HttpWebRequest, HttpWebResponse, StreamReader and IAsyncResult.
  • HttpWebRequest: This object is used to send http web request to web application server. You can use BeginGetResponse and EndGetResponse method of HttpWebRequest object to get response asynchronously.
  • HttpWebResponse: This object is used to get response from web application server.
  • StreamReader: This object is used to get response stream.
  • IAsyncResult: This object is used to send and retrieve asynchronous http web request.


We created HttpWebRequest object using WebRequest.Create method. After creating object, I called BeginGetResponse method to send the http request to web application server. After calling this method, our application execution resume because we used asynchronous method for Http Web Request.

When you need web response then you can use the HttpWebRequest object and call EndGetResponse method. After getting response successfully, we converted it in stream and later on in string variable.

If web applications use “POST” method to send the request data then you have to change the method little bit.

///
/// Gets the response with post.
///

/// <param name="StrURL">The URL.
/// <param name="strPostData">The post data.
/// HTML Result
protected string GetResponseWithPost(string StrURL, string strPostData)
{
string strReturn = "";
HttpWebRequest objRequest = null;
ASCIIEncoding objEncoding = new ASCIIEncoding();
Stream reqStream = null;
HttpWebResponse objResponse = null;
StreamReader objReader = null;
try
{
objRequest = (HttpWebRequest)WebRequest.Create(StrURL);

objRequest.Method = "POST";
byte[] objBytes = objEncoding.GetBytes(strPostData);
objRequest.ContentLength = objBytes.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
reqStream = objRequest.GetRequestStream();
reqStream.Write(objBytes, 0, objBytes.Length);

IAsyncResult ar = objRequest.BeginGetResponse(new AsyncCallback(GetScrapingResponse), objRequest);
//// Wait for request to complete
ar.AsyncWaitHandle.WaitOne(1000 * 60 * 3, true);
if (objRequest.HaveResponse == false)
{
throw new Exception("No Response!!!");
}
objResponse = (HttpWebResponse)objRequest.EndGetResponse(ar);
objReader = new StreamReader(objResponse.GetResponseStream());
strReturn = objReader.ReadToEnd();

}
catch (Exception exp)
{
throw exp;
}
finally
{
objRequest = null;
objEncoding = null;
reqStream = null;
if (objResponse != null)
objResponse.Close();
objResponse = null;
objReader = null;
}
return strReturn;
}

///
/// Gets the scraping response.
///

/// <param name="result">The result.
protected void GetScrapingResponse(IAsyncResult result)
{

}


I created one more method “GetResponseWithPost”. This method actually sends data using Post method to web server. This method takes two parameters: URL and PostData. URL is just an URL of any web application. URL is not contains any query string data. Another parameter PostData contains data to post on the web server.

We need following changes to work with POST method:
  • We need one more parameter to pass Post Data
  • Need to assign “POST” to Http Web Request method property
  • Need to set ContentLength and ContentType
  • Create Stream before sending actual web request to web server

I think you have question, how to know the POST data while submitting web request to web server using browser. For this you can use LiveHTTPHeader add-ons in Firefox. This add-ons display all the post data when you access the web application using Firefox.

4 comments:

  1. Excellent.........
    Now its clear in my mind,
    Thanks

    - Nikita Thakkar
    Sr. Software Engineer,
    Ahmedabad

    ReplyDelete
  2. Great stuff

    Thanks Hardik

    In both examples, what is the purpose of calling the method GetScrapingResponse?

    Rick

    ReplyDelete
  3. What if I want to pass the parameter using GET?

    ReplyDelete
  4. how to send request by authentication (i.e with user name and password) to server and get response from it?

    ReplyDelete

DotNet Code Guru