【问题标题】:Unable to submit an HTTPS post with Java无法使用 Java 提交 HTTPS 帖子
【发布时间】:2016-05-02 15:39:23
【问题描述】:

我正在编写一些工具(仅打算在开发环境中使用),我正在尝试自动将消息提交到 HTTP Post。我的方法(如下)sendByHTTPSNaive() 使用 javax.net.ssl.TrustManager 的幼稚实现来接受任何证书。 (同样,这是用于开发测试。)我收到了 401 未经授权的代码。下面是我的方法和我相信的 TLS 握手的调试输出。项目网络人员告诉我,我只面临一个 xml 网关,天真的 TrustManager 应该通过该网关,但向我保证没有其他身份验证。如果它是一些 TLS 问题,或者实际上除了 xml 网关之外还有另一个身份验证层,我已经不知道如何调试这个问题了?

public String sendByHTTPSNaive(String destinationURI,
                                String msgBodyContent) {

    String result = "no response";
    try {
        URL url = new URL(destinationURI);
        // URLConnection con = url.openConnection();

        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

        // specify that we will send output and accept input
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setConnectTimeout(20000); // long timeout, but not infinite
        con.setReadTimeout(20000);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);

        NoopHostnameVerifier HOSTNAME_VERIFIER = new NoopHostnameVerifier();
        TrustManager[] TRUST_MANAGER = { new NaiveTrustManager() };
        if (con instanceof HttpsURLConnection) {

            SSLContext context = SSLContext.getInstance("TLS");
            context.init(new KeyManager[0], TRUST_MANAGER,
                                            new SecureRandom());
            SSLSocketFactory socketFactory = context.getSocketFactory();
            ((HttpsURLConnection) con).setSSLSocketFactory(socketFactory);
            ((HttpsURLConnection) con).setHostnameVerifier(HOSTNAME_VERIFIER);
        }

        // tell the web server what we are sending
        con.setRequestProperty("Content-Type", "text/xml");
        OutputStreamWriter writer = new OutputStreamWriter(
                                        con.getOutputStream());
        writer.write(msgBodyContent);
        writer.flush();
        writer.close();

        // reading the response
        InputStreamReader reader = new InputStreamReader(con.getInputStream());
        StringBuilder buf = new StringBuilder();

        char[] cbuf = new char[2048];
        int num;

        while (-1 != (num = reader.read(cbuf))) {
            buf.append(cbuf, 0, num);
        }

        result = buf.toString();
    } catch (Throwable t) {
        t.printStackTrace(System.out);
    }
    return result;
}

使用标志 -Djavax.net.debug=all 调试输出

HTTP/1.1 401 Una
uthorized..Serve
r: Apache-Coyote
/1.1..WWW-Authen
ticate: Basic re
alm="L7SSGBasicR
ealm"..L7-Policy
-URL: https://ab
cdefghi.jklmno.c
om:443/ssg/polic
y/disco?serviceo
id=20938758..Con
tent-Length: 23.
.Date: Thu, 28 A
pr 2016 16:00:56
GMT....Authenti
cation RequiredC
..._.2..5\"B....
:...............

【问题讨论】:

    标签: java-8 tls1.2


    【解决方案1】:

    您的 TLS 工作正常,错误来自 HTTP。您需要在请求中提供身份验证信息 - 特别是 Authorization 标头。响应包含一个“WWW-Authenticate”标头——它要求客户端提供凭据。客户端应该使用适当的“授权”标头重复请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2015-05-11
      • 2018-04-29
      • 2018-09-21
      • 2019-06-20
      相关资源
      最近更新 更多