![](/img/trans.png)
[英]org.apache.http.impl.client.CloseableHttpClient Proxy Authentication
[英]Accessing HTTPS web services through proxy tunnel using org.apache.http.impl.client.ProxyClient
我需要通過http代理隧道訪問一些https soap服務。 所以我正在使用org.apache.http.impl.client.ProxyClient通過代理隧道連接到目標主機。 這給我返回了一個通過代理服務器連接到目標主機的套接字。 返回的套接字已正確連接到目標系統。 現在,我需要調用目標系統中托管的soap服務。 但是我不知道如何通過套接字訪問那些https服務。 下面是我的示例程序。 需要調用的服務的示例網址https://XX.XX.XX.XX:44330 / sampleService / 1.0
public static void proxyTunnelDemo(String url,String soapRequestBody) throws IOException, HttpException {
ProxyClient proxyClient = new ProxyClient();
HttpHost target = new HttpHost("XX.XX.XX.XX", 44330);
HttpHost proxy = new HttpHost("YY.YY.YY.YY", 9293);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("********", "********");
Socket socket = proxyClient.tunnel(proxy, target, credentials);
// Need to access Web service through socket .
// request method GET
// PORT = 44330
// url = https://XX.XX.XX.XX:44330/sampleService/1
//soapRequestBody = soap message needs to be sent.
String SOAP_CREDENTIALS = "*********";
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket)factory.createSocket(socket, target.getHostName(), target.getPort(), true);
sslSocket.startHandshake();
try {
//Send header
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), StandardCharsets.UTF_8));
String authorization = new BASE64Encoder().encode(SOAP_CREDENTIALS.getBytes());
// You can use "UTF8" for compatibility with the Microsoft virtual machine.
wr.write("GET / HTTP/1.1\r\n");
wr.write("Host: "+target.getHostName()+"\r\n");
wr.write("Content-Length:"+ soapRequestBody.length() + "\r\n");
wr.write("Content-Type: text/html\"\r\n");
wr.write("SOAPAction:"+url+"\r\n");
wr.write("Authorization: Basic "+authorization+"\r\n");
wr.write("\r\n "); //Send data
wr.write(soapRequestBody);
wr.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(sslSocket.getInputStream(), StandardCharsets.UTF_8));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} finally {
socket.close();
sslSocket.close();
}
}
隨着上面的代碼得到下面的錯誤響應
HTTP / 1.1 400錯誤X后端傳輸:失敗失敗內容類型:text / xml連接:關閉
您已經在端口44330上與XX.XX.XX.XX建立了連接。接下來,您需要將此套接字傳遞給SSLSocket,如下所示
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket)factory.createSocket(socket, host, port, true);
然后,您可以與網絡服務器進行SSL握手
socket.startHandShake();
然后使用printwriter從套接字讀取/寫入
https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
完整示例如下:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.