Java SOAP客户端数据丢失

时间:2014-04-29 09:09:31

标签: java ssl soap https

我有一个通过HTTPS发送数据的Java客户端,数据是SOAP 1.1。我没有使用任何特定的技术,如JAX-WS。使用的方法很简单。发送SOAP响应时,我从错误流中得到500错误。看起来当我运行Java时会发回500错误响应,并且当运行Java之外的Soap Sonar时,响应是完美的。它看起来有些数据在到达Web服务之前就丢失了。如何调试?

以下是代码:

public void sendSoap(String endpoint) {
     HttpsURLConnection httpsUrlConnection = null;
     URL url = new URL(endpoint);
     httpsUrlConnection = (HttpsURLConnection) url.openConnection();
     httpsUrlConnection.setRequestMethod("POST");
     httpsUrlConnection.setDoOutput(true);
     httpsUrlConnection.setRequestProperty("Content-Type", "application/xml");
     SSLContext sc = SSLContext.getInstance("SSL");
     sc.init(null, getTrustManager(), new SecureRandom());
     httpsUrlConnection.setSSLSocketFactory(sc.getSecureSocketFactory());
     httpsUrlConnection.connect();
     try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsUrlConnection.getOutputStream());
        outputStreamWriter.write(buildData());
        outputStreamWriter.flush();
        outputStreamWriter.close();
        InputStreamReader inputStreamReader = new InputStreamReader(httpsUrlConnection.getInputStream());
    }
    catch(Exception e) {
        InputStream inputStream = httpsUrlConnection.getErrorStream();
        inputStreamReader i = new InputStreamReader(inputStream);
        BufferedReader j = new BufferedReader(i);
        int read = j.read();
        while(read != -1) {
            String line = j.readLine();
            System.out.println(line);
        }
    }
   }

public String buildData() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(new String("<?xml version='1.0' encoding='utf-8'?>"));
        stringBuilder.append(new String("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:ns='http://somenamespace.com'>"));
        stringBuilder.append(new String("<soap:Body>"));
        stringBuilder.append(new String("<ns:service>"));
        stringBuilder.append(new String("<ns:message>message</ns:message>"));
        stringBuilder.append(new String("<ns:username>username</ns:username>"));
        stringBuilder.append(new String("<ns:password>password</ns:password>"));
        stringBuilder.append(new String("</ns:service>"));
        stringBuilder.append(new String("</soap:Body>"));
        stringBuilder.append(new String("</soap:Envelope>");
    }

 public TrustManager[] getTrustManager() {
   TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
       public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {

       }

       public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
           System.out.println("AuthType : " + " " + string);
           for(int i = 0; i < xcs.length; i++) {
               System.out.println("\t" + xcs[i].getIssuerX500Principal().getName());
               System.out.println("\t" + xcs[i].getIssuerDN().getName());
           }
       }

       public X509Certificate[] getAcceptedIssuers() {
           return null;
       }
   }};
   return trustAllCerts;
 }

从buildData()复制构造的String并通过Soap Sonar运行时,它可以工作。

编辑:

这是我从错误流中收到的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
       <title>500 - Internal server error.</title>
       <style type="text/css">
       <!--
            ody{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
           ieldset{padding:0 15px 10px 15px;} 
           1{font-size:2.4em;margin:0;color:#FFF;}
           2{font-size:1.7em;margin:0;color:#CC0000;} 
           3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
           header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
           ackground-color:#555555;}
           content{margin:0 0 0 2%;position:relative;}
           content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
      ->
      </style>
  </head>
 <body>
   <div id="header"><h1>Server Error</h1></div>
  <div id="content">
  <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset>
 </div>
 </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为您的直接问题出在服务器而不是客户端。 &#34;内部服务器错误&#34;表明服务器以某种方式错误处理请求。您是否可以访问服务器和/或其日志?试着弄明白它在收到你的请求时到底做了什么。

答案 1 :(得分:0)

事实证明,HttpURLConnection类的getInputStream()方法在HTTP状态代码&gt; = 400时抛出IOException。因此,一旦抛出IOException,就无法访​​问getInputStream()方法。一旦返回HTTP状态代码&gt; = 400,就可以从getErrorStream()方法访问SOAP Fault本身。这可以通过以下方式实现:

HttpURLConnection httpConn = (HttpURLConnection)urlConnection; 
InputStream is; 
if (httpConn.getResponseCode() < 400) { 
   is = httpConn.getInputStream(); 
} 
else { 
/* error from server */ 
   is = httpConn.getErrorStream(); 
}