Using the HttpWebRequest class

Posted by Tim on January 14, 2009 in ASP.NET, C# |

In this particular scenario I had to initiate a request from another website in code and track its response to be modified and rendered back to the user.

I had a series of LinkButton html controls on the web form that consume one event.

<asp:LinkButton id=”link1” runat=”server” onClick=”genericEvent”></asp:LinkButton>

When the user clicks on a particular button, ASP.Net invoked the genericEvent event

Protected void genericEvent(object sender, EventArgs e)
{
LinkButton thisLink = (LinkButton)sender;
}

I have a Dictionary object that holds the ID of the LinkButton and a URL value

Dictionary<String,String> Dict = new Dictionary<String,String>();
Dict.Add(“ID”,http://www.google.co.nz);

I created a helper method that initates the HttpWebRequest class to return a response based on the Dictionary ID and URL chosen from the user using the LinkButton.ID property.

Private void GetResponse(String ID)
{
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(DicLinks[ID]);
WebResponse newResponse = newRequest.GetResponse();
newResponse.Close();
}

Protected void genericEvent(object sender, EventArgs e)
{
LinkButton thisLink = (LinkButton)sender;
GetResponse(thisLink.ID);
}

This proved very useful in the scenario of getting various types of information from different sources that can be manipulated and rendered into one view for the user.

References:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

Tags:

Leave a Reply

Copyright © 2008-2012 Codelab Blog All rights reserved.
Desk Mess Mirrored version 1.9.1 theme from BuyNowShop.com.