Java HTTP proxy server

I need to implement an HTTP proxy server application that will proxy requests from multiple clients to remote servers

Here are the steps:

>Client forwards request to proxy > proxy forwards request to server > server returns request to proxy > proxy returns request to client

I just don't know how I should implement this agent My first idea is to implement a Tomcat application using Jersey / Apache httpclient to forward the request to the remote server and return the response to the client?

Is there a better way to implement such a proxy server?

The agent will need to handle multiple threads

Solution

You can't implement it as a servlet, and there's no reason to use any form of HTTP client

A featureless proxy server is a very simple thing:

>Accept a connection and start a thread. > Read request from client to blank line. > Extract the get or connect command or any of its contents and connect to the specified host. > If it fails, send back an appropriate HTTP error response, close the socket and forget it. > Otherwise, start two threads to copy bytes, one in each direction Nothing fancy, just

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer,count);
}

>When one socket reads EOS, please close the other socket for output and exit the thread that gets EOS. > If the socket as the EOS source has closed the output, close it

Or use Apache squid

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>