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

时间:2016-11-28 21:30:21

标签: 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)

1 个答案:

答案 0 :(得分:0)

也许你必须设置:     httpPost.setHeader(" Content-type"," test / xml"); 成     httpPost.setHeader(" Content-type"," text / xml"); 差异是test / xml到text / xml