我在製作依賴於設置http.proxyPort
和http.proxyHost
的Java應用程序。有兩個過程:一個是正常程序,另一個是代理。我有一個簡單的套接字偵聽器,運行在http.proxyPort
(我控制)上。它是那樣簡單Java https代理(使用https.proxyPort和https.proxyHost)
while (true) {
try {
Socket connection = server.accept();
Handler handler = new Handler(connection);
handler.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
所以每當 「處理1」 發出HTTP請求 - 像
URL yahoo = new URL("http://www.google.ca/");
URLConnection yc = yahoo.openConnection();
System.out.println(yc.getClass().getName());
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
它去通過代理。現在如果客戶端使用HTTPS協議呢?喜歡改用https://google.ca
?有一個屬性https.proxyPort
和https.proxyHost
,但我真的一直在努力幾個月(開,關,這不是太重要),沒有運氣。我讀過一堆線程(我會在最後列出一些,所以你知道我已經做了一些事情)。
到目前爲止我最親密的嘗試: 服務器
try {
System.setProperty("javax.net.ssl.keyStore", "test.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "2520xe");
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =
(SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
System.out.println("Ready");
SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
InputStream inputstream = sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
OutputStream toClient = sslsocket.getOutputStream();
toClient.write(("HTTP/1.0 200 Connection established\n" +
"Content-Length: " + "Shut down!".getBytes().length
+ "\r\n").getBytes("utf-8"));
toClient.write("Shut down!".getBytes("utf-8"));
toClient.close();
} catch (Exception exception) {
exception.printStackTrace();
}
客戶
try {
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9999");
URL yahoo = new URL("https://www.google.ca/");
URLConnection yc = yahoo.openConnection();
System.out.println(yc.getClass().getName());
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
而且我得到這個錯誤javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
我GOOGLE了,但與一些郵件的東西,而不是走了過來。
基本上,我需要創建一個Java代理服務器,這是由https.proxyPort
和https.proxyHost
標誌設置到客戶端,並且可以將數據發送回客戶端應用程序,這可能不以任何方式被修改(它只是用URL connection = new URL("https://...")
)
幾個我試過的網站...
- creating a Java Proxy Server that accepts HTTPS
- http://stilius.net/java/java_ssl.php
- 還有一件事讓Java接受所有證書,但我找不到任何鏈接。我有代碼,但我遇到了比我現在正在做的事情更多的錯誤,但如果有幫助,我可以包括它(我最初並不是因爲這已經是一個長期問題了)
目前我還設置了系統屬性https.proxyHost掙扎。通過將調試器附加到VM進程,似乎虛擬機根本沒有拿起https.proxyHost屬性。 – 2014-04-30 10:51:04
好的,只需使用proxyHost和proxyPort而不使用http。或https。前綴現在適合我。它也似乎是(再一次)從Java 6到Java 7的行爲。從appleis到Java 7的上述語句。 – 2014-04-30 12:57:21
當通過代理請求https URL時,客戶端必須請求'CONNECT www.google.ca:443 HTTP/1.0'和代理服務器響應'HTTP/1.0 200連接建立',兩者都是純文本格式。然後客戶端開始SSL握手。 – auntyellow 2014-11-26 13:27:15