Click here to Skip to main content
Click here to Skip to main content

HttpTunnel in C#

, 29 Jan 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create TCp connection through proxy server

Download source code - 2.1 Kb

Introduction

Some time we need to establish TCP connection through proxy server, where our client host is behind proxy or firewall and deny the TCP connection to outside world. It just allows only HTTP connection. HTTP Tunnel is the method from which we can go through HTTP and connect to outside world as a TCP connection.

Description

When we want create HTTP connection through proxy server, the client sends a request to the proxy server using HTTP protocol CONNECT method that it wants to establish a connection to some host (Destination) on some port. Now proxy server if allows tries to establish a connection to destination host by sending a request on behalf of client and receives the response which in turns send it back to client. If the negative response is received from destination it sends negative HTTP response to the client and closes the connection. And if the positive response is received, it sends a positive HTTP response to the client and the connection is considered to be established. Now the proxy server is acting as a Tunnel and dose not care what data is transferring between client and destination. Due to lack of time I have not tested it 100%, if you find any bug please email me so that I can rectify it.

Protocol

HTTP protocol specifies the CONNECT method through which we can create a connection between client and destination. The client will first opens a connection with the proxy server. And after that it must send a CONNECT request in the form of HTTP request with some required parameter.
CONNECT <Destination ip>:<Destination Port> HTTP/1.0\nProxy-Authorization:Basic <\n><\n>
The proxy server process the request and tries to make the connection to

<destionation_address>:<destination_port>

After that proxy server will send HTTP response, If it positive response that is with CODE=200 then it is considered that connection is established. And if response dose not include code=200 then connection is closed. Here user id and password should be in the form of base64 encoding, I have written another class to convert Proxy user id and password in base64 encoding format.

Usage of Code

There are two class file in this application httptunnel.cs and Base64Encoder.cs. you just need to add these classes in your application and call a method createHttpTunnel which will accept 5 parameter.
(string proxyipadd,string port,string struid,string strpwd,string destip,string destport)

proxyipadd – Proxy Ip.
port - Proxy Port.
struid - Proxy Uid.
strpwd - Proxy Pwd.
destip - Destination IP(e.g. 202.109.67.123).
destport - Destination Port..

Suppose you need to connect to 202.109.67.123 on port 25 where your proxy IP is 192.168.0.101 and proxy port is 808. you just need to create an object of HTTP_TUNNEL class nad call method createHttpTunnel.

HTTP_TUNNEL  objHTTP_TUNNEL = new HTTP_TUNNEL();
Socket s = objHTTP_TUNNEL. createHttpTunnel("192.168.0.101", "808", "proxyid", "proxypwd", "202.109.67.123", "25");

In my next article we will discuss how to establish TCP connection using SOCKs proxy.

About Kamlesh

Dotnet developer working with software Development Company in India.
Achieved Master of Computer Application and Bachelor degree in commerce from Mumbai University, India.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kamlesh Singh
Software Developer
India India
No Biography provided

Comments and Discussions

 
Questionre: comment PinmemberVop-Nels'fvkingscreamoblood Vampz Lovetodeath17-Dec-13 21:54 
QuestionError Pinmemberkamleshgs30-Nov-13 4:07 
QuestionError PinmemberAlireza.imani19-May-13 23:07 
Question"In my next article we will discuss how to establish TCP connection using SOCKs proxy" Pinmemberacturcato21-Nov-12 8:23 
GeneralMy vote of 1 PinmemberRajeev Dhiman13-Aug-12 21:49 
GeneralThis works. Pinmemberoberon200611-Feb-11 6:52 
GeneralMy vote of 5 Pinmembertirx5-Oct-10 13:37 
GeneralFinally an example to use sockets with a proxy Pinmembertirx5-Oct-10 13:37 
GeneralMy vote is 5, but it's hard to understand how we can use it. But after.. thank you. PinmemberMidax17-May-10 6:20 
GeneralHTTP Tunneling Pinmembermshauny26-Oct-09 22:48 
QuestionHelp me please Pinmemberniktus22-Oct-09 2:18 
Generalgood article Pinmemberzhb_lzh12-Feb-09 16:59 
Questionhow to make UDP tunnel? Pinmemberqhgary7-Sep-08 22:21 
Questionusing proxy server for filtering URL request Pinmemberprajapati.dhananjay3-Feb-07 9:50 
AnswerRe: using proxy server for filtering URL request PinmemberKamlesh_Singh23-Feb-07 22:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 29 Jan 2007
Article Copyright 2007 by Kamlesh Singh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid