简体   繁体   English

Java Http代理无法正常工作

[英]Java Http Proxy does not work correctly

I try to implement a Java Proxy for Http (Https will be the extension after Http works). 我尝试为Http实现Java代理(Https将是Http工作后的扩展名)。 I found a lot of resources on the Internet and try to solve all problems on my own so far. 我在Internet上找到了很多资源,并尝试自己解决所有问题。 But now I come to a point where I stuck. 但是现在我陷入了困境。 My Proxoy does not load the full http websites. 我的Proxoy无法加载完整的http网站。 I get a lot of error messages with the socket is already closed. 套接字已关闭,我收到很多错误消息。 So I think I try to send something over a Socket that is closed. 所以我想我尝试通过关闭的套接字发送一些信息。

My Problem is now. 现在是我的问题。 I can not see why it is like this. 我不明白为什么会这样。 I think a lot over the problem but I can not find the mistake. 我对这个问题有很多思考,但是我找不到错误。 From my side The Sockets only get closed when the server close the connection to my Proxy Server. 从我的角度来看,只有在服务器关闭与我的代理服务器的连接时,套接字才会关闭。 This happen when I read a -1 on the input stream from the server. 当我从服务器读取输入流上的-1时,就会发生这种情况。

I would be happy for any help :-) 我很乐意提供任何帮助:-)

greetings Christoph 问候克里斯托夫

public class ProxyThread extends Thread {
Socket client_socket;
Socket server_socket;
boolean thread_var = true;
int buffersize = 32768;

ProxyThread(Socket s) {
    client_socket = s;
}

public void run() {
    System.out.println("Run Client Thread");
    try {
        // Read request
        final byte[] request = new byte[4096];
        byte[] response = new byte[4096];

        final InputStream in_client = client_socket.getInputStream();
        OutputStream out_client = client_socket.getOutputStream();

        in_client.read(request);
        System.out.println("---------------------- Request Info --------------------");
        System.out.println(new String(request));        

        Connection conn = new Connection(new String(request));
        System.out.println("---------------------- Connection Info --------------------");
        System.out.println("Host: " + conn.host);
        System.out.println("Port: " + conn.port);
        System.out.println("URL: " + conn.URL);
        System.out.println("Type: " + conn.type);
        System.out.println("Keep-Alive:" + conn.keep_alive);
        server_socket = new Socket(conn.URL, conn.port);
        InputStream in_server = server_socket.getInputStream();
        final OutputStream out_server = server_socket.getOutputStream();
        out_server.write(request);
        out_server.flush();

        Thread t = new Thread() {
            public void run() {
              int bytes_read;
              try {
                while ((bytes_read = in_client.read(request)) != -1) {
                      out_server.write(request, 0, bytes_read);
                      out_server.flush();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
          };
          t.start();

        int bytes_read;
        while ((bytes_read = in_server.read(response)) != -1) {
            out_client.write(response, 0, bytes_read);
            out_client.flush();
            //System.out.println("---------------------- Respone Info --------------------");
            //System.out.println(new String(response));
        }

        //System.out.println("EIGENTLICH FERTIG");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            client_socket.close();
            server_socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

EDIT: My HTTP Proxy now works. 编辑:我的HTTP代理现在可以工作。 The Answer is pretty helpfull once you understand what is ryl going on. 一旦您了解了瑞尔发生了什么,答案就会很有帮助。 If you come hear to find a solution this questions may help you: 如果您来这里寻求解决方案,则此问题可能会帮助您:

  • Does the client send a request only to one Website / Webserver? 客户是否仅将请求发送到一个网站/ Web服务器? Means do we always have the same port / hostname? 意味着我们总是具有相同的端口/主机名吗?

  • The Loop from the answer is very usefull but think where to place it? 答案中的“循环”非常有用,但您认为应该放在哪里?

Last think: Thanks @EJP its working your reply was very usefull. 最后想一想:感谢@EJP,您的回复非常有用。 It only tooks a time to understand it! 只花了一点时间就了解了!

You are making all the usual mistakes, and a few more. 您正在犯所有通常的错误,还有其他一些错误。

  1. The entire request is not guaranteed to arrive in a single read. 不能保证整个请求都能一次读取。 You can't assume more than a single byte has arrived. 您不能假设已经到达一个字节以上。 You have to loop. 你必须循环。

  2. You aren't checking for end of stream at this stage. 您目前不在检查流的结尾。

  3. You need a good knowledge of RFC 2616 to implement HTTP, specifically the parts about Content-length and transfer encoding. 您需要具备RFC 2616的丰富知识才能实现HTTP,特别是有关内容长度和传输编码的部分。

  4. You cannot assume that the server will close the connection after sending the response. 您不能假定服务器在发送响应后将关闭连接。

  5. Closing either the input or the output stream or a socket closes the socket. 关闭输入流或输出流或套接字会关闭该套接字。 This is the reason for your SocketException: socket closed . 这就是您的SocketException: socket closed的原因SocketException: socket closed

  6. When you get to HTTPS you will need to look at the CONNECT verb. 使用HTTPS时,您需要查看CONNECT动词。

  7. Flushing a socket output stream does nothing, and flushing inside a loop is to be avoided, 冲洗套接字输出流没有任何作用,并且应避免在循环内冲洗,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM