Click here to Skip to main content
14,241,236 members

HttpTunnel in C#

Rate this:
2.79 (18 votes)
Please Sign up or sign in to vote.
2.79 (18 votes)
29 Jan 2007CPOL
How to create TCp connection through proxy server

Download source code - 2.1 Kb


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.


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.


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 <proxy><\n><\n></proxy>
The proxy server process the request and tries to make the connection to


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.
destport - Destination Port..

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

Socket s = objHTTP_TUNNEL. createHttpTunnel("", "808", "proxyid", "proxypwd", "", "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.


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


About the Author

Kamlesh Singh
Software Developer
India India
No Biography provided

Comments and Discussions

QuestionSend Connect request as a client Pin
Kiaksarg31-Aug-15 12:38
memberKiaksarg31-Aug-15 12:38 
Questionre: comment Pin
Vop-Nels'fvkingscreamoblood Vampz Lovetodeath17-Dec-13 20:54
memberVop-Nels'fvkingscreamoblood Vampz Lovetodeath17-Dec-13 20:54 
QuestionError Pin
kamleshgs30-Nov-13 3:07
memberkamleshgs30-Nov-13 3:07 
QuestionError Pin
Alireza.imani19-May-13 22:07
memberAlireza.imani19-May-13 22:07 
Question"In my next article we will discuss how to establish TCP connection using SOCKs proxy" Pin
acturcato21-Nov-12 7:23
memberacturcato21-Nov-12 7:23 
GeneralThis works. Pin
oberon200611-Feb-11 5:52
memberoberon200611-Feb-11 5:52 
GeneralMy vote of 5 Pin
tirx5-Oct-10 12:37
membertirx5-Oct-10 12:37 
GeneralFinally an example to use sockets with a proxy Pin
tirx5-Oct-10 12:37
membertirx5-Oct-10 12:37 
GeneralMy vote is 5, but it's hard to understand how we can use it. But after.. thank you. Pin
Midax17-May-10 5:20
memberMidax17-May-10 5:20 
GeneralHTTP Tunneling Pin
mshauny26-Oct-09 21:48
groupmshauny26-Oct-09 21:48 
QuestionHelp me please Pin
niktus22-Oct-09 1:18
memberniktus22-Oct-09 1:18 
Generalgood article Pin
zhb_lzh12-Feb-09 15:59
groupzhb_lzh12-Feb-09 15:59 
Questionhow to make UDP tunnel? Pin
qhgary7-Sep-08 21:21
memberqhgary7-Sep-08 21:21 
Questionusing proxy server for filtering URL request Pin
prajapati.dhananjay3-Feb-07 8:50
memberprajapati.dhananjay3-Feb-07 8:50 
AnswerRe: using proxy server for filtering URL request Pin
Kamlesh Singh23-Feb-07 21:09
memberKamlesh Singh23-Feb-07 21:09 

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

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

Posted 17 Jan 2007


45 bookmarked