当前位置: 首页 > 知识库问答 >
问题:

具有客户端证书连接的HTTP客户端的SOAP请求超时异常

安浩瀚
2023-03-14

我正在尝试访问具有客户端证书的url,并使用以下内容生成密钥:

keytool -genkey -alias server -keyalg RSA -keystore /example.jks -validity 10950

和密钥存储:

keytool -import -trustcacerts -alias root -file /example.cer -keystore /example.jks

并尝试连接:

System.out.println("------------------------------------------- In         SendRequest ------------------------------------################");
SSLContext context = SSLContext.getInstance("TLS");
Certificate cert=getCertificate();
URL url = new URL("url");
URLConnection urlConnection = url.openConnection();
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
SSLSocketFactory sslSocketFactory = getFactory();
httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
DataOutputStream wr = new         DataOutputStream(httpsUrlConnection.getOutputStream());
System.out.println(wr.toString());
File req_xml = new File("request.xml");
//SOAPMessage req = TestCase.createSoapSubsribeRequest("SUBSCRIBE");
HttpPost post = new HttpPost("url");
post.setEntity(new InputStreamEntity(new FileInputStream(req_xml), req_xml.length()));
post.setHeader("Content-type", "text/xml; charset=UTF-8");
//post.setHeader("SOAPAction", "");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);

LOG.info("************************************************************RESPONSE****************"+response.getStatusLine());
// SOAP response(xml) get        String res_xml = EntityUtils.toString(response.getEntity());
    LOG.info("Response"+res_xml);
}
private SSLSocketFactory getFactory( )  {
    try{ 
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        System.out.println("------------------------------------------- In getFactory ------------------------------------################");
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        //InputStream keyInput = new FileInputStream(pKeyFile);
        String password = "obsmesh";
                    char[] passwd = password.toCharArray(example.jks");
        keystore.load(is, passwd);
        // keyInput.close();
        keyManagerFactory.init(keystore, password.toCharArray());
        System.out.println("------------------------------------------- In jsdkl ------------------------------------################");
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManager[] trust = null;
        context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
        return context.getSocketFactory();
}catch(Exception e){
    System.out.println(e);
}
return null;

}

共有1个答案

羊慈
2023-03-14

尝试使用此代码,我希望它将对您有所帮助。

     KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom()
            .loadTrustMaterial(new File("//your jks file path "), "//key password here",
                    new TrustSelfSignedStrategy())
            .build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslcontext,
            new String[] { "TLSv1" },
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    CloseableHttpClient httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .build();
    try {


     File req_xml = new File("// your request xml file path");


   HttpPost post = new HttpPost("//https client url");
   post.setEntity(new InputStreamEntity(new FileInputStream(req_xml), req_xml.length()));
   post.setHeader("Content-type", "text/xml; charset=UTF-8");

        System.out.println("Executing request " + post.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(post);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
            System.out.println(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
 类似资料:
  • 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的。您可以查看 HTTP 协议 了解更多相关信息。 以下是来自于浏览器端的重要头信息,您可以在 Web 编程中频繁使用: 头信息 描述 Accept 这个头信息指定浏览器或其他客户端可以处理的 MIME 类型。值 image/png 或 image/jpeg 是最常

  • 问题内容: 我在ElastiCache上运行Redis集群。 多个进程连接到Redis集群。每个进程都位于Docker容器中。流程不尽相同-我有一个流程,一个流程等。 正常运行几天后,连接到Redis时,我的某些进程开始超时。当我进入受影响的容器并尝试通过到达群集时,与群集的连接超时。这告诉我,问题不仅影响过程,而且影响整个容器。 当我从任何其他容器中使用时,连接都不会出现问题。 我的进程会根据需

  • 问题内容: 我有一个Java应用程序通过SSL套接字通过套接字连接到另一个Java应用程序,因此我的客户端JVM已经设置了和属性。 此应用程序需要向需要客户端身份验证的Web服务器发出一些HTTP请求。我可以使用Java返回一个来打开连接。 我要在请求中提供给Web服务器的客户端证书与设置为我的JVM系统属性的客户端证书不同。有没有一种方法可以设置客户端证书。作为请求属性? 问题答案: 直接通过的

  • 我正在使用Apache HTTP客户端联系外部服务。这项服务可能需要几个小时(如果不是更长的话)才能产生响应。我尝试了一些不同的方法,但要么以套接字结束,要么以读取超时结束。我刚刚尝试使用RequestConfig将套接字和连接超时设置为0,根据文档,这应该是无限的,但请求总是在1小时后返回。有什么想法吗?

  • 客户端的HTTP/HTTPS请求。 进程:主进程​ ClientRequest是由EventEmitter来实现Writable Stream​ new ClientRequest(options) 作用:发起新的HTTP/HTTPS请求 options(Object | String) - options是String时即请求URL。 options 是Object时则按以下属性请求: meth

  • 问题内容: 如何在Go HTTP客户端中使空闲超时? 空闲超时意味着从HTTP客户端内部调用Conn接口的Read / Write方法时发生超时。当客户端下载文件并且由于读取超时而导致下载失败时,此功能很有用。 问题答案: 您需要创建自己的,并返回,以设置适当的读写期限。 该会是这个样子: