Relay a request from client to Server via Proxy using sockets c#

I have custom tcp server listening on port 5888(dummy port). The proxy server listens for incoming connections. When the proxy receives a HTTP Request, it should relay the same request to a different server.

Eg:

Proxy receives: http://proxyserver.com/mypage.html Proxy should Relay: http://MainServer.com/mypage.html

The response from the Main Server should be sent directly to the requesting client.

I have used sockets for accepting connections and parsing the raw HTTP Request before relaying the new HTTP request to the main server. This is becoming too complicated, since I have to send the Raw HTTP Request to the main server, get the response and send it to the client machine.The proxy is basically becoming a middle-man. I want the proxy only for the one side of communication ie to change the host address to the MainServer and then the main server should take care of sending the response to the requesting client.

How to achieve this ?


HTTP works over a TCP connection initiated from a client to a server. In presence of proxy you need at least two connections - one from client to the proxy, and one from proxy to the server. These two connections are sort of "independent" in a sense that both transmit standard HTTP requests and responses. Proxy here acts as a client to the end server.

What you want involves at least three TCP connections, and requires sending HTTP response on a different connection from the one where initiating HTTP request came on. That is outside of the HTTP proper - the client wouldn't know that it needs a second connection somewhere else, and how to match requests on one connection to responses on the other. The best you can do within HTTP is redirecting to a different server, say, with a special generated URL or something.

Just remember that HTTP response has to come on the same TCP connection as the original request.

链接地址: http://www.djcxy.com/p/62490.html

上一篇: 获取Windows代理用户名/密码

下一篇: 使用套接字通过代理将请求从客户端转发到服务器c#