Java HTTPS连接转发 [英] Java HTTPS Connection Forwarding

查看:135
本文介绍了Java HTTPS连接转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的程序,在 Java 中接收来自浏览器(如Firefox)的连接请求,解析统计信息请求,然后将请求转发给原始目的地。程序然后从目标读取响应,解析响应的统计信息,然后将响应转发给浏览器。

I'm writing a simple program in Java that receives connection requests from a browser (like Firefox), parses the request for statistical info, then forwards the request to the original destination. The program then reads the response from the destination, parses the response for statistical info, then forwards the response to the browser.

此操作的伪代码如下:

The pseudo code of this operation is as follows:

// Accept connection from browser and read request
1. Socket browserConnection = serverSocket.accept();
2. browserConnection.getInputStream().read(buffer);
3. SocketInetAddress destInetAddress = parseHttpRequest(buffer);

// Connect to destination and forward request
4. Socket destConnection = new Socket(destInetAddress);
5. destConnection.getOutputStream().write(buffer);

// Read response from destination
6. destConnection.getInputStream().read(buffer);
7. parseHttpResponse(buffer);

// Forward response to browser
8. browserConnection.getOutputStream().write(buffer);

这适用于HTTP连接,但我得到连接重置用于HTTPS连接。

This works well with HTTP connections, but I'm getting connection reset for HTTPS connections.

注意:我知道HTTP和HTTPS连接之间的区别,与HTTP不同,它不是只需一次发送,然后一些收到。我的HTTPS 代码根据需要读取,并且根据需要写入

NOTE : I know the difference between HTTP and HTTPS connections, that unlike HTTP, it's NOT just a one-time send and then some receives. My code for HTTPS reads as much as needed, and also writes as much as needed.

为什么我从任何HTTPS服务器获得连接重置(例如 https://www.google。 com https://www.comodo.com 等)我尝试连接?!

Why am I getting connection resets from any HTTPS server (e.g. https://www.google.com, https://www.comodo.com, etc.) I try to connect ?!

推荐答案

使用HTTPS代理,浏览器将发送 CONNECT 命令代理与目标服务器建立TCP连接(例如 https://www.google.com )。代理建立连接后,它会向浏览器返回确定消息。然后,浏览器将启动与目标服务器的SSL握手,以启动加密数据传输。代理不得干扰数据。代理需要做的就是在浏览器和目标服务器之间中继字节流,就是这样。

With a HTTPS proxy, the browser will send a CONNECT command for the proxy to establish a TCP connection to the destination server (e.g. https://www.google.com). After the proxy establishes the connection, it returns a OK message to the browser. Then the browser will start the SSL handshake with the destination server to initiate encrypted data transfer. The proxy must not interfere with the data. All that the proxy needs to do is to relay the flow of bytes between the browser and the destination server, that's it.

这篇关于Java HTTPS连接转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆