繁体   English   中英

使用org.apache.http.impl.client.ProxyClient通过代理隧道访问HTTPS Web服务

[英]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

完整示例如下:

https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/samples/sockets/client/SSLSocketClientWithTunneling.java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM