HttpWebRequest example with error handling (C#)

This C# example explains how to GET or POST a request to a web server using the .NET framework classes HttpWebRequest and HttpWebResponse. The code is embedded in a command line example program, where a specified URL is requested using the HTTP method "GET", and the response is written to a specified file.

To use the code for example on an aspx page, just cut out the WRequest() function and catch the return string. A VB.Net version of the function is also available.

The WRequest() parameters are: URL, HTTP_method, POST_data
  • URL: Any valid URL.
  • HTTP_method: Use "GET" to make a normal request or "POST" to submit additional (form) data along with the request.
  • POST_data: An empty string if HTTP_method "GET" is used, a string of POST data if HTTP_method "POST" is used. The format is "param1=value1&param2=value2"
using System;
using System.IO;
using System.Net;
using System.Text;

public class HttpWebRequestTool
  public static void Main(String[] args)
    if (args.Length < 2)
      Console.WriteLine("Missing argument. Need a URL and a filename");
      StreamWriter sWriter = new StreamWriter(args[1]);
      sWriter.Write(WRequest(args[0], "GET", ""));

  public static string WRequest(string URL, string method, string postData)
    string responseData = "";
      System.Net.CookieContainer cookieJar = new System.Net.CookieContainer();
      System.Net.HttpWebRequest hwrequest =
        (System.Net.HttpWebRequest) System.Net.WebRequest.Create(URL);
      hwrequest.CookieContainer = cookieJar;
      hwrequest.Accept = "*/*";
      hwrequest.AllowAutoRedirect = true;
      hwrequest.UserAgent = "http_requester/0.1";
      hwrequest.Timeout= 60000;
      hwrequest.Method = method;
      if (hwrequest.Method == "POST")
        hwrequest.ContentType = "application/x-www-form-urlencoded";
        // Use UTF8Encoding instead of ASCIIEncoding for XML requests:
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] postByteArray = encoding.GetBytes(postData);
        hwrequest.ContentLength = postByteArray.Length;
        System.IO.Stream postStream = hwrequest.GetRequestStream();
        postStream.Write(postByteArray, 0, postByteArray.Length);
      System.Net.HttpWebResponse hwresponse =
        (System.Net.HttpWebResponse) hwrequest.GetResponse();
      if (hwresponse.StatusCode == System.Net.HttpStatusCode.OK)
        System.IO.Stream responseStream = hwresponse.GetResponseStream();
        System.IO.StreamReader myStreamReader =
          new System.IO.StreamReader(responseStream);
        responseData = myStreamReader.ReadToEnd();
    catch (Exception e)
      responseData = "An error occurred: " + e.Message;
    return responseData;

Use the following command (from a folder containing the source file) to compile the program:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /out:HttpWebThingie.exe HttpWebThingie.cs

Note: The path to the .NET framework compiler may not be exactly like above on all systems.



RSS 2.0 feed All content
RSS 2.0 feed ajax
RSS 2.0 feed asp
RSS 2.0 feed aspnet
RSS 2.0 feed bicycle
RSS 2.0 feed copenhagen
RSS 2.0 feed databases
RSS 2.0 feed denmark
RSS 2.0 feed diy
RSS 2.0 feed dotnet
RSS 2.0 feed html
RSS 2.0 feed japan
RSS 2.0 feed javascript
RSS 2.0 feed modding
RSS 2.0 feed photography
RSS 2.0 feed utilities
RSS 2.0 feed vbscript