How does Java implement proxy server through Sockt?


Sandra Pique


There are two types of HTTP proxies:

RFC 7230 - http / 1.1: the general proxy described in Message Syntax and routing (i.e. the revised RFC 2616, the first part of HTTP / 1.1 protocol). This proxy plays the role of "middleman". For the client connected to it, it is the server; for the server to be connected, it is the client. It is responsible for transmitting HTTP messages back and forth between the two ends.

Tunneling TCP based protocols through web proxy servers is a tunneling proxy described by the HTTP protocol body (body) complete the communication and implement any TCP based application layer protocol proxy in the way of HTTP. This proxy uses the connect method of HTTP to establish the connection, but the connect is not a part of RFC 2616 - http / 1.1 at first. The description of connect and tunnel proxy is not added until the revised version of HTTP / 1.1 released in 2014. See RFC 7231 - http / 1.1: Seman for details tics and Content。 In fact, such proxies have long been widely implemented.

http proxy 

The HTTP request passes through the proxy server, and the proxy server is only responsible for forwarding the corresponding HTTP response body.

HTTPS proxy

When the HTTPS request passes through the proxy server, it will send a connect message to establish a tunnel with the proxy server. If the proxy server returns HTTP 200, the establishment is successful. The subsequent proxy server is only responsible for forwarding data. In fact, the SSL / TLS handshake still occurs on the client and the real server.


Create a socketserver listening port. According to the HTTP request header method, if connect is an HTTPS request, otherwise it is an HTTP request. Then establish a connection between the proxy server and the target server according to the host header, and then forward data. The HTTPS request needs special processing because the connect request does not need to be forwarded. It needs to return an HTTP 200 response, establish a tunnel, and then forward it.

Recent posts