我需要通过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-Backside-Transport:FAIL FAIL 内容类型:text / xml 连接:关闭
答案 0 :(得分:0)
您已经在端口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
此处的完整示例: