简体   繁体   English

Java中的简单代理:POST请求异常

[英]Simple Proxy in Java : POST request exception

I wrote a simple HTTP proxy in java. 我用Java编写了一个简单的HTTP代理。 The code is as follows: 代码如下:

public class SampleProxy {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;
        int port = 1234;
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("Port Error");
            System.exit(-1);
        }
        while (listening) {
            new ProxyThread(serverSocket.accept()).start();
        }
        serverSocket.close();
    }
}

class ProxyThread extends Thread {

    private final Socket clientSocket;

    public ProxyThread(Socket socket) {
        this.clientSocket = socket;
    }

    public void run() {
        try {
            String parts[];
            // Read request
            InputStream incomingIS = clientSocket.getInputStream();
            int incomingLen = incomingIS.available();

            byte[] b = new byte[8196];

            int len = incomingIS.read(b);

            if (len > 0) {

                System.out.println("The Request is : \n" + new String(b, 0, len) + "\n*********\n");

                Socket socket = new Socket("localhost", 80);

                OutputStream outgoingOS = socket.getOutputStream();

                outgoingOS.write(b, 0, len);

                OutputStream incomingOS = clientSocket.getOutputStream();
                InputStream outgoingIS = socket.getInputStream();


                int length;
                //Read from server
                byte[] b2=new byte[8196];
                length = outgoingIS.available();

                outgoingIS.read(b2);

                incomingOS.write(b2, 0, b2.length);

                incomingOS.close();
                outgoingIS.close();
                outgoingOS.close();
                incomingIS.close();

                socket.close();
            } else {
                incomingIS.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            Logger.getLogger(ProxyThread.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

As seen, the client requests at port 1234, the proxy forwards the request to the server and gets the response which is then returned back to the client. 如图所示,客户端在端口1234上请求,代理将请求转发到服务器并获得响应,然后将响应返回给客户端。

In normal scenarios, this works fine, but, I tried testing the same for high traffic scenarios using a shell script which sends multiple(around 50) POST requests, one after another and found out that for some requests, only the headers are getting received. 在正常情况下,这工作得很好,但是,我尝试使用shell脚本,它发送多(约50)POST请求, 一个又一个测试相同的高流量的情况,结果发现,对于一些要求,越来越只获得了头。 The POST data is omitted but the Content-Length shows the appropriate length of the variables which ought to be sent. 省略了POST数据,但是Content-Length显示了应该发送的变量的适当长度。 This does not happen for specific requests, but at random, maybe 3 or 4 out of 50 Requests. 对于特定的请求不会发生这种情况,但是会随机发生,可能是50个请求中的3或4。

The same error however, does not seem to exist for GET requests. 但是,对于GET请求似乎不存在相同的错误。

Any suggestions as to whats actually causing the error? 关于实际上导致错误的原因有什么建议吗?

Usual mistakes here. 这里常见的错误。 Misuse of available(), and failure to actually implement the HTTP protocol correctly. 滥用available(),并且无法正确实现HTTP协议。 You need to read RFC 2616 thoroughly, especially the parts about request and response length; 您需要通读RFC 2616,尤其是有关请求和响应长度的部分; and reread the Javadoc for InputStream.available(), where you will find a specific warning against using it as you are doing here. 并重新阅读InputStream.available()的Javadoc,您将在此处找到针对使用它的特定警告。

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

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