Listening for an HTTP Request

I have an appointment where I need to create a proxy server that will manipulate some requests / responses that it receives, implement caching, etc.

Firstly, I want to create a simple proxy that simply passes all requests and responses. I did a few searches on the Internet, and I'm a little confused about how to listen for requests on a specific port and receive HTTP requests. I stumbled upon the Socket , ServerSocket , HttpURLConnection , but I'm not sure how all this interacts. I tried to read the documents, but they are all intertwined and a little difficult to understand.

Can you point me in the right direction, which classes should I use for this purpose, and perhaps share a snippet to listen on the port, get the headers of HTTP requests, etc.?

+6
source share
2 answers

Well, I can only assume that your proxy will be a ServerSocket, which will listen for requests on the HTTP port. You are reading the request through the server socket input stream. After verifying that the request matches the proxy rules, you open the HttpConnection for the real HTTP server and use the output stream in the http connection, which you send the client request, and then using the http connection input stream, you read the real HTTP protocol Server response, which you ultimately send back to the client using the socket output stream.

In the proxy server, since you intercept requests and responses, you can manipulate them before forwarding.

Sounds right?

+5
source

Here are some introductory Java sockets: http://www.oracle.com/technetwork/java/socket-140484.html

+1
source

Source: https://habr.com/ru/post/885614/


All Articles