Java implementation HTTP Proxy (4)

Source: Internet
Author: User

Next, we'll focus on dispatcher because this class is the key to implementation
The following method completes forwarding, which is the HttpRequest
First we created a socket based on the specified host and port. The socket is connected to the real host
and then we call the request method, which forwards the request, which is simpler, simply by writing the header to the outputstream of the socket.
Step Three, we use the Httpresponsefactory read method to read and encapsulate a HttpResponse object from the InputStream of the socket.
Step Fourth, both the response header and the response stream are written to the client. The client's outputstream can be obtained from the HttpRequest object.
   

    /** * @param request-void */public void dispatch (HttpRequest request, httpresponse response, Proxyhos
        T proxyhost) {String host = Proxyhost.gethost ();
        int port = Proxyhost.getport ();

        Socket socket = NULL;
            try {socket = This.createsocket (host, Port);
            Socket.setsotimeout (10000);
            Socket.setkeepalive (FALSE);
            InputStream Socketinputstream = Socket.getinputstream ();

            OutputStream Socketoutputstream = Socket.getoutputstream (); if (logger.isdebugenabled ()) {//Logger.debug ("------------------request.header\r\n" + reques
            T.tostring ());
            This.request (Request, Socketoutputstream);

            HttpResponse HttpResponse = Httpconnectionfactory.createhttpresponse (Socketinputstream, SocketOutputStream); if (HttpResponse!= null) {Response.sethttpheader (httpResponse.gethttpheader (). Clone ());
                Response.setchunked (httpresponse.getchunked ());
                Response.setcontenttype (Httpresponse.getcontenttype ());
                Response.setstatus (Httpresponse.getstatus ());
                Response.setreasonphrase (Httpresponse.getreasonphrase ());
                This.resonse (Request, HttpResponse, Request.getoutputstream ());
            Response.setcontentlength (Httpresponse.getcontentlength ()); The catch (IOException e) {if (logger.iserrorenabled ()) {L
            Ogger.error (Request.getrequesturl (), E);
                    } finally {if (socket!= null) {try {
                Socket.close (); The catch (IOException e) {}}}}


Here is the request method:
1. First, we need to modify the HTTP request header, mainly to modify the request host and port number, otherwise the server will think that the request is not itself, because the client sent the request is to our agent request, and in fact we are just a proxy, so we need to modify it, and send it to the real host.
2. Will request the hair to the service period, only is writes the request header string to the OutputStream inside.

3. Send the client's other request data to the server, such as POST request or upload file. But different requests send the data is not the same, so our program must be able to know how much data the client sent, we use Contentpump.pumpcontentstream () This method to send the request data to the server, it is like a pump, Draw the data in a pipe into another pipe. But it must know beforehand how much to smoke, otherwise, blocking IO will be blocked when the data is read to the end.

    /** * @param request * @param outputstream * @throws ioexception/private void request (HttpR Equest request, OutputStream OutputStream) throws IOException {String host = Inetaddress.getlocalhost (). Getho

        Staddress ();
    
            if (usexforwardedfor) {String xforwardedfor = Request.getheader ("X-forwarded-for");
            if (xforwardedfor = = null) {xforwardedfor = host;
            else {xforwardedfor = "," + host; }//Local:not set HTTP_CLIENT_IP request.setheader ("Http_client_ip", request.getremoteaddr
            ());
        Request.setheader ("X-forwarded-for", xforwardedfor);
        String headers = request.tostring ();
        Outputstream.write (Headers.getbytes ());
        Outputstream.flush ();
    Httpstream.pipe (Request.getinputstream (), OutputStream, Request.getcontentlength ()); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.