Java SOAP 客户端数据丢失

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

最佳答案

我认为您的直接问题出在服务器而不是客户端。 “内部服务器错误”表明服务器以某种方式错误处理了请求。您有权访问服务器和/或其日志吗?尝试弄清楚它在收到您的请求时到底在做什么。

关于Java SOAP 客户端数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360316/

相关文章:

Java:将图像添加到 JFrame 中的 JPanel

java - 键入时,从存储的现有文本中将单词(文本)插入到 JTextArea 中

iOS:使用 SharePoint Server 之前的 NTLM 身份验证

java - 从 SOAP 信封中检索类对象?

java - 在 HTTP Preview 上运行 Web 项目时出现错误 : "Could not find JRE executable" on MAC OS X Yosemite

java - 从网络应用程序,如何在没有用户交互的情况下在客户端机器上运行 exe

ssl - 如何在 Window 7 中使用 TLS 配置 RDP

apache - 如何将页面重定向到 https 和站点的 'www' 版本

ssl - 为 SSLVerifyClient 要求异常(exception)

c# - 单个应用程序中的 SOAP 服务和 JSF/Icefaces