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

Http Post

Usage

  • Example
  • Example Code
    • Utils.Http.Post("TargetUrl""DataToPost");
      

The Post function :

public static string Post(string pUrl, string pPostData)
{
	HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(pUrl);
	webRequest.ContentType = "application/x-www-form-urlencoded";
	webRequest.Method = "POST";
	byte[] bytes = System.Text.Encoding.UTF8.GetBytes(pPostData);
	Stream requestWriter = webRequest.GetRequestStream(); //GetRequestStream
	requestWriter.Write(bytes, 0, bytes.Length);
	requestWriter.Close();
 
	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)
	{
		throw new Exception("Could not post. " + 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: