Click here to Skip to main content
15,885,155 members
Articles / Programming Languages / C#
Article

HttpTunnel in C#

Rate me:
Please Sign up or sign in to vote.
2.79/5 (18 votes)
29 Jan 2007CPOL3 min read 95.3K   3.9K   45   15
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 <proxy><\n><\n></proxy>
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)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSend Connect request as a client Pin
Kiaksarg31-Aug-15 12:38
Kiaksarg31-Aug-15 12:38 
Questionre: comment Pin
Vop-Nels'fvkingscreamoblood Vampz Lovetodeath17-Dec-13 20:54
Vop-Nels'fvkingscreamoblood Vampz Lovetodeath17-Dec-13 20:54 
QuestionError Pin
kamleshgs30-Nov-13 3:07
kamleshgs30-Nov-13 3:07 
QuestionError Pin
Alireza.imani19-May-13 22:07
Alireza.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
acturcato21-Nov-12 7:23 
GeneralThis works. Pin
oberon200611-Feb-11 5:52
oberon200611-Feb-11 5:52 
GeneralMy vote of 5 Pin
tirx5-Oct-10 12:37
tirx5-Oct-10 12:37 
GeneralFinally an example to use sockets with a proxy Pin
tirx5-Oct-10 12:37
tirx5-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
Midax17-May-10 5:20 
GeneralHTTP Tunneling Pin
mshauny26-Oct-09 21:48
mshauny26-Oct-09 21:48 
QuestionHelp me please Pin
niktus22-Oct-09 1:18
niktus22-Oct-09 1:18 
Generalgood article Pin
zhb_lzh12-Feb-09 15:59
zhb_lzh12-Feb-09 15:59 
Questionhow to make UDP tunnel? Pin
qhgary7-Sep-08 21:21
qhgary7-Sep-08 21:21 
Questionusing proxy server for filtering URL request Pin
prajapati.dhananjay3-Feb-07 8:50
prajapati.dhananjay3-Feb-07 8:50 
AnswerRe: using proxy server for filtering URL request Pin
Kamlesh Singh23-Feb-07 21:09
Kamlesh 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.