Code Snippets
Code Samples

The Blog
Our Projects

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

Tutorial - HTTP Proxy
Category:Internet & Networks
Uploaded at:28-May-03
This article assumes some (Not that much is required) experience in HTTP and sockets programming. And shows in a straight forward way how to speak with HTTP Proxy servers.
Accessing a website through HTTP Proxy is pretty simple, it's almost like accessing a site directly.
Let's assume that you want to make a connection to www.sourceforge.net, using a direct connection you would create a new socket, and use it to connect directly to the site. The sent headers would probably look like this:

GET /somedocument.html HTTP/1.1
Host: www.sourceforge.net
Connection: close
Accept: */*
User-Agent: MyINetApp/

Note that the HTTP client is said to be v1.1 compliant, so if your client is only meant to work with the 1.0 protocol, your headers will look a bit different.

Using a proxy server to make the connection isn't very different, instead of connecting to www.sourceforge.net, you will make a connection to the HTTP proxy server (The port used for Proxy servers is usually 8080, but not always) The sent headers to a proxy server aren't very different, here's the syntax:

GET http://www.sourceforge.net/somedocument.html HTTP/1.1
Host: www.sourceforge.net
Connection: close
Accept: */*
User-Agent: MyINetApp/
Cache-Control: no-store, no-cache
Pragma: no-cache

Notice that on the request line, and the Host header now specify the domain name as well, that tells the proxy server what site it should connect to. You should also notice the two extra headers at the bottom, these two should be used when connecting to a proxy server, to make sure the server is sending you the latest version of the document.

Proxy Authentication:

Some proxy servers require that the user authenticates itself, before it can use it. That is done with another header, named Proxy-Authorization. There are a couple of methods to authenticate to a proxy server, we're only going to talk about Basic, which is used by most proxy servers out there. Here's the syntax:
Proxy-Authorization: Basic (base64)([username]:[password])
Don't forget that "Basic" is case-sensitive to a lot of Proxy servers, so make sure you type it correctly. The second part of the header, is the username and password, seperated by a colon, encoded in base64 (Base64 goes a bit out of the scope of this tutorial, but you can find a base64 encoder at the code sample of this tutorial). Just one sentence about Base64: it's being used much over the Internet because it's a simple binary/text data encryption/decryption techinque, which in generally makes 8 bit-bytes to 6 bit-bytes and vice versa. For example: If my username is "useme", and my password is: "test", the string that should be encoded is: useme:test After encoding, it'll look like this (in Base64 ofcourse): dXNlbWU6dGVzdA==
Following the last example, if I would also like to authenticate under "useme" using the password "test", these will be the headers I'd send:

GET http://www.sourceforge.net/somedocument.html HTTP/1.1
Host: www.sourceforge.net
Connection: close
Accept: */*
User-Agent: MyINetApp/
Proxy-Authorization: Basic dXNlbWU6dGVzdA==
Cache-Control: no-store, no-cache
Pragma: no-cache

That's all you have to know in order to use HTTP Proxy servers in your programs, you may now download the sample I wrote and see some action going on!
If you want to learn more about HTTP you can always read the RFC's at w3.org!
Another link I recommend you to visit is HTTP Made Really Easy.

   Kudos to Saar(torando@goblineye.com) for writing this k3wl tutorial!
User Contributed Comments(1)
 [1] lindanne | 2014-06-16 03:40:58

Perfect. I thank you for your contributions on this issue! http://www.frivrelax.com
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
::Top 5 Tutorials::
Embedded Python[105388]
2D Rotated Rectangles Collision Detection[78700]
Keyboard Hook[68402]
HTTP Proxy[35813]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10558]
PS2 Mouse Driver[6059]
Wave Format Player[4945]
Reading FAT12[4826]

All rights reserved to RageStorm © 2009