I am a danish programmer living in Bangkok.
Read more about me @ rasmus.rummel.dk.
Webmodelling Home > ASP.NET > C# Utility Functions > Http - RequestResponse

Http - RequestResponse

Usage

  • Example
    • I use the RequestResponse function in my Facebook API Tutorial then requesting an OAuth access_token from the Facebook API. In that same tutorial I am using the RequestResponse function once again to request a Facebook users friend list from the Facebook API.
  • Example Code
    • string responseData = Utils.Http.RequestResponse("TargetUrl"); //the TargetUrl will typically contain a query string to describe the data requested
      

The RequestResponse function :

public static string RequestResponse(string pUrl)
{
	HttpWebRequest webRequest = System.Net.WebRequest.Create(pUrl) as HttpWebRequest;
	webRequest.Method = "GET";
	webRequest.ServicePoint.Expect100Continue = false;
	webRequest.Timeout = 20000;
 
	Stream responseStream = null;
	StreamReader responseReader = null;
	string responseData = "";
	try
	{
		WebResponse webResponse = webRequest.GetResponse();
		responseStream = webResponse.GetResponseStream();
		responseReader = new StreamReader(responseStream);
		responseData = responseReader.ReadToEnd();
	}
	catch (Exception exc)
	{
		HttpContext.Current.Response.Write("<br /><br />ERROR : " + exc.Message);
	}
	finally
	{
		if (responseStream != null)
		{
			responseStream.Close();
			responseReader.Close();
		}
	}
 
	return responseData;
}

Comments

You can comment without logging in
Profile
Username
Password
Password
Email
Nickpic
Get notified on reply to own posts  (only works if you specify an email address)
Get notified on receiving a PM  (only works if you specify an email address)
Remember my username
Remember my password
signature
Words: Chars: Chars left: