Java 套接字已关闭 - 发布 SOAP 请求时

标签 java soap

第一次在此网站上发布问题。我一直在开发一个项目,在该项目中,我必须生成签名( header x509)的 SOAP 请求并将其提交到 Web 服务所在的 https URL。到目前为止,我已经能够生成签名的 SOAP 请求,但是当我发布请求时,我收到套接字已关闭异常。任何人都可以帮我找出我的代码缺少什么可能导致此错误。预先感谢您!

下面是我如何发布请求的代码以及错误:

            SSLContext sslcontext = SSLContexts.custom()
                    .loadTrustMaterial(new File("my keystore"), "mypassword".toCharArray(),
                            new TrustSelfSignedStrategy())
                    .build();

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    new String[] { "TLSv1" },
                    null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(300 * 1000)
                    .setConnectTimeout(300 * 1000)
                    .setConnectionRequestTimeout(300 * 1000)
                    .build();
            HttpClient httpclient = HttpClients.custom() //CloseableHttpClient 
                    .setDefaultRequestConfig(requestConfig)
                    .setSSLSocketFactory(sslsf)
                    .build();

            HttpPost httpPost;
            httpPost = new HttpPost("https://my webservice");

            httpPost.setHeader("Content-type", "test/xml");

            File input = new File(esbXmlFile);

            InputStream inputStream = new FileInputStream(input);
            InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);
            httpPost.setEntity(inputStreamEntity);


            String content = null;
            int statusCode = 1; //statusCode is response code form WS.
            try
             {
                 HttpResponse response = httpclient.execute(httpPost);
                 statusCode = response.getStatusLine().getStatusCode();
                 System.out.println("statusCode : " + statusCode);
                 HttpEntity entity = response.getEntity();
                 content = EntityUtils.toString(entity);

             }
            catch (SocketException e) {
                e.getStackTrace();
            }
            catch (IOException e) {
                 e.getStackTrace();
             }

 I get this error: 
            DEBUG (main) [org.apache.http.conn.ssl.SSLConnectionSocketFactory] - Starting handshake
        2016-11-28 16:24:04,019 DEBUG (main) [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] - http-outgoing-1: Shutdown connection
        2016-11-28 16:24:04,019 DEBUG (main) [org.apache.http.impl.execchain.MainClientExec] - Socket is closed
        java.net.SocketException: Socket is closed
                at java.net.Socket.setSoLinger(Socket.java:986)
                at org.apache.http.impl.BHttpConnectionBase.shutdown(BHttpConnectionBase.java:305)
                at org.apache.http.impl.conn.DefaultManagedHttpClientConnection.shutdown(DefaultManagedHttpClientConnection.java:97)
                at org.apache.http.impl.conn.LoggingManagedHttpClientConnection.shutdown(LoggingManagedHttpClientConnection.java:89)
                at org.apache.http.impl.conn.CPoolEntry.shutdownConnection(CPoolEntry.java:74)
                at org.apache.http.impl.conn.CPoolProxy.shutdown(CPoolProxy.java:96)
                at org.apache.http.impl.execchain.ConnectionHolder.abortConnection(ConnectionHolder.java:127)
                at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:352)
                at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
                at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
                at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
                at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
                at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
                at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
                at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)

最佳答案

也许你必须设置: httpPost.setHeader("内容类型", "test/xml"); 进入 httpPost.setHeader("内容类型", "text/xml"); 区别是 test/xml 变成了 text/xml

关于Java 套接字已关闭 - 发布 SOAP 请求时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40853843/

相关文章:

java - Hibernate Spatial - 遇到无效的字节序标志值

java - 如何在放心中获得大于指定值的值

c# - 在 C# 中将 Soap 请求 header 从 text/xml 更改为 soap/xml for asmx webservice

java - 阅读Web服务,我有一个WSDL

java - 避免 Hibernate 中某些方法的持久化

java - 我应该在请求中创建 executorService 还是在 Web 应用程序中共享一个实例?

java - 无法弄清楚我的逻辑错误Java

python - 无法在 zeep、python 中使用 SOAP 服务

Java Soap 通过 https 连接

c# - SOAP 客户端没有完成它的工作或者可能没有返回任何东西?