Simple C# HTTP proxy class

February 12th, 2010 | posted by admin | Programmingread users' comment (0)

We’re gonna create a simple C# class for doing HTTP requests using proxy. It will only have 2 methods: one for HTTP GET requests and other for POST requests. No error handling is done here so you’ll have to implement your own.  The code is self-explanatory.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace blogtest
{
 class Program
 {
  static void Main(string[] args)
  {
   simplehttp http = new simplehttp();
   //testing on ipcheckit.com so we can make sure we're really connecting through proxy. Look for proxy IP in html printed in console
   //remember to change 127.0.0.1, 8080, login, password to your working proxy details
   string html = http.geturl("http://ipcheckit.com/", "127.0.0.1", 8080, "login", "password");
   System.Console.WriteLine(html);
  }
 }

class simplehttp
{
 public string geturl(string url, string proxyip, int port, string proxylogin, string proxypassword)
 {
  HttpWebResponse resp;
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  req.UserAgent = "Mozilla/5.0";
  req.AllowAutoRedirect = true;
  req.ReadWriteTimeout = 5000;
  req.CookieContainer = new CookieContainer();
  req.Referer = "";
  req.Headers.Set("Accept-Language", "en,en-us");
  StreamReader stream_in;

  WebProxy proxy = new WebProxy(proxyip, port);
  //if proxylogin is an empty string then don't use proxy credentials (open proxy)
  if (proxylogin != "") proxy.Credentials = new NetworkCredential(proxylogin, proxypassword);
  req.Proxy = proxy;

  string response = "";
  try
  {
   resp = (HttpWebResponse)req.GetResponse();
   stream_in = new StreamReader(resp.GetResponseStream());
   response = stream_in.ReadToEnd();
   stream_in.Close();
  }
  catch (Exception ex)
  {
  }
  return response;
 }

 public string getposturl(string url, string postdata, string proxyip, short port, string proxylogin, string proxypassword)
 {
  HttpWebResponse resp;
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  req.UserAgent = "Mozilla/5.0";
  req.AllowAutoRedirect = true;
  req.ReadWriteTimeout = 5000;
  req.CookieContainer = new CookieContainer();
  req.Method = "POST";
  req.ContentType = "application/x-www-form-urlencoded";
  req.ContentLength = postdata.Length;
  req.Referer = "";

  WebProxy proxy = new WebProxy(proxyip, port);
  //if proxylogin is an empty string then don't use proxy credentials (open proxy)
  if (proxylogin != "") proxy.Credentials = new NetworkCredential(proxylogin, proxypassword);
  req.Proxy = proxy;

  StreamWriter stream_out = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
  stream_out.Write(postdata);
  stream_out.Close();
  string response = "";

  try
  {
   resp = (HttpWebResponse)req.GetResponse();
   Stream resStream = resp.GetResponseStream();
   StreamReader stream_in = new StreamReader(req.GetResponse().GetResponseStream());
   response = stream_in.ReadToEnd();
   stream_in.Close();
  }
  catch (Exception ex)
  {
  }
  return response;
 }
 }
}
more