Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java代理-can';t正确地从HTTP GET/POST请求交换数据_Java_Sockets_Http Post - Fatal编程技术网

Java代理-can';t正确地从HTTP GET/POST请求交换数据

Java代理-can';t正确地从HTTP GET/POST请求交换数据,java,sockets,http-post,Java,Sockets,Http Post,从一开始,我尝试实现一个小型代理服务器,它同时处理GET和POST请求(只需将Handler类替换为以下类): 在转发数据(…)中。我不知道如何才能发现,流是否可以提供一些数据,或者如何避免read的阻塞调用 有人知道我做错了什么,知道我如何正确地转发数据,以便所有浏览器都能正确加载内容而不会出现不必要的延迟吗 看,问题是您正在使用forwardData()将服务器响应复制到客户端,因为它不涉及内容长度:字段 HTTP/1.1{HTTP/1.1 200 OK Cache-Control: no-

从一开始,我尝试实现一个小型代理服务器,它同时处理
GET
POST
请求(只需将
Handler
类替换为以下类):

转发数据(…)
中。我不知道如何才能发现,流是否可以提供一些数据,或者如何避免
read
的阻塞调用


有人知道我做错了什么,知道我如何正确地转发数据,以便所有浏览器都能正确加载内容而不会出现不必要的延迟吗

看,问题是您正在使用
forwardData()
将服务器响应复制到客户端,因为它不涉及
内容长度:
字段

HTTP/1.1{HTTP/1.1 200 OK
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html; Charset=utf-8
Content-Encoding: gzip
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
X-RADID: P301511016-T104210110-C24000000000248666
P3P: CP="BUS CUR CONo FIN IVDo ONL OUR PHY SAMo TELo"
Date: Tue, 15 Dec 2015 09:33:41 GMT
Content-Length: 1656

<1656 Bytes>

尽管上面的代码目前不是100%有效,但它应该会让您了解如何继续。如果内容长度为null,并且收到一个空行,您可能还需要关闭流。

这无疑让我向前迈出了一大步。谢谢你的帮助!
if ((read = inputStream.read(buffer)) > 0) {
HTTP/1.1{HTTP/1.1 200 OK
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html; Charset=utf-8
Content-Encoding: gzip
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
X-RADID: P301511016-T104210110-C24000000000248666
P3P: CP="BUS CUR CONo FIN IVDo ONL OUR PHY SAMo TELo"
Date: Tue, 15 Dec 2015 09:33:41 GMT
Content-Length: 1656

<1656 Bytes>
private static void forwardData(int threadId, Socket inputSocket, Socket outputSocket, Integer length, boolean isPost) {
    int cLength = -1;
    int count = 0;
    try {
        InputStream inputStream = inputSocket.getInputStream();
        try {
            OutputStream outputStream = outputSocket.getOutputStream();
            try {
                byte[] buffer = new byte[4096];
                int read;
                if (length == null || length > 0) {
                    do {
                        if ((read = inputStream.read(buffer)) > 0) { // search for "Content-Length: "
                            if (cLength == -1) {
                                String response = new String(buffer, "UTF-8");
                                int pos = response.indexOf("Content-Length:");
                                if (pos > 0) {
                                    String lString = response.substring(pos + 16, pos + 24).replaceAll("([0-9]*).*\\n?\\r?.*", "$1");
                                    cLength = Integer.parseInt(lString);
                                }
                            }

                            if (cLength != -1) { // if length is given, count bytes from empty line on
                                if (count > 0) { // already started  - so just add
                                    count = count + read;
                                } else { // check if empty line exists,  "\r\n\r\n" or "\r\r"
                                    for (int n = 0; n < read; n++) {
                                        if (buffer[n] == 13 && buffer[n + 1] == 13) {
                                            count = read - (n + 2); // if so, set count to bytes read after the empty line
                                        }
                                        if (buffer[n] == 13 && buffer[n + 1] == 10 && buffer[n + 2] == 13 && buffer[n + 3] == 10) {
                                            count = read - (n + 4); // same as above
                                        }
                                    }
                                }
                            }

                            outputStream.write(buffer, 0, read);
                            if (inputStream.available() < 1) {
                                outputStream.flush();
                            }
                            if (length != null) {
                                length = length - read;
                            }
                        }
                    } while (read >= 0 && (length == null || length > 0) && (cLength == -1 || count < cLength));
                }

            } finally {
                if (!outputSocket.isOutputShutdown()) {
                    if (!isPost) {
                        outputSocket.shutdownOutput();
                    }
                }
            }
        } finally {
            if (!inputSocket.isInputShutdown()) {
                inputSocket.shutdownInput();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}