简体   繁体   English

在java和SOAPUI中使用Web服务

[英]Consuming a web service in java and SOAPUI

I am consuming a webservice in java. 我正在使用java中的web服务。 This service is using UserNameToken, Timestamp and Signature for message level security. 此服务使用UserNameToken,Timestamp和Signature来实现消息级安全性。 I have been given SoapUI project file(xml) to try and get the data from the service. 我已经获得了SoapUI项目文件(xml)来尝试从服务中获取数据。 Everything works fine in SoapUI. 在SoapUI中一切正常。

Now when I am trying to use the same soapUI file to generate the artifacts, I am getting a message "Receiver Policy falsified" . 现在,当我尝试使用相同的soapUI文件生成工件时,我收到一条消息"Receiver Policy falsified" Why is that I can connect to the service in soapUI, but I can not in java? 为什么我可以连接到soapUI中的服务,但我不能在java中?

So I found out that I am not sending a keystore using Transport Layer Security. 所以我发现我没有使用传输层安全性发送密钥库。 How do I send this in a SOAP request. 如何在SOAP请求中发送它。 I have done similar setting in SOAP UI in SSL settings and it works there. 我在SSL设置中的SOAP UI中做了类似的设置,它在那里工作。

This is my code for SOAP request in java 这是我在java中的SOAP请求代码

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String url = "https://abc.org/test/ApplicantInformationService"; // Test System Web Service URL

SSLSocketFactory sslSocketFactory = getSSLSocketFactory();

conn = urlOnly.openConnection();
if (conn instanceof HttpsURLConnection) {
     ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
     ((HttpsURLConnection) conn).setRequestMethod("POST");
 } 

conn.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");    
conn.setDoOutput(true);
SOAPMessage message = createSOAPRequest(user);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
message.writeTo(os1);
String requestXml = new String(os1.toByteArray()); 

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(requestXml);
out.flush();
if(out != null){
    out.close();
}               
String line = "";                
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String responseNewWay = "";
while ((line = in.readLine()) != null) {
    responseNewWay = responseNewWay + line;
}
//SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(user), url);
//soapConnection.close();
//ByteArrayOutputStream os = new ByteArrayOutputStream();
//soapResponse.writeTo(os);
//String responseXml = new String(os.toByteArray());

And SSLFactory Code is like this 而SSLFactory Code就是这样的

  private static SSLSocketFactory getSSLSocketFactory() 
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException,  UnrecoverableKeyException, IOException, KeyManagementException{
    SSLSocketFactory sslsf = null;                  
    String keyStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";           

        String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();        
    String keyStoreType = "JKS";
        String keyStorePassword = "mypassword";
        String trustStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";        

        String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath();
        String trustStorePassword = "mypassword";
        String alias = "clientcertificate";

        Properties systemProps = System.getProperties();
        systemProps.put("javax.net.ssl.keyStore", keyStorePath);
        systemProps.put("javax.net.ssl.keyStorePassword", keyStorePassword);
        systemProps.put("javax.net.ssl.keyStoreType", keyStoreType);

        systemProps.put("javax.net.ssl.trustStore", trustStorePath);
        systemProps.put("javax.net.ssl.trustStoreType", "JKS");
        systemProps.put("javax.net.ssl.trustStorePassword", trustStorePassword);
        System.setProperties(systemProps); 

        KeyManager[] keyManagers = createKeyManagers(keyStoreFileName,keyStorePassword, alias);
        TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword);
        sslsf = initItAll(keyManagers, trustManagers);

        return sslsf;
    }

And the error SOAP response i am getting is like this 我得到的错误SOAP响应是这样的

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <soapenv:Fault>
            <soapenv:Code>
                <soapenv:Value>soapenv:Receiver</soapenv:Value>
            </soapenv:Code>
            <soapenv:Reason>
                <soapenv:Text xml:lang="en-US">Policy Falsified</soapenv:Text>
            </soapenv:Reason>
            <soapenv:Role>https://abc.org/test/ApplicantInformationService</soapenv:Role>
            <soapenv:Detail>
                <l7:policyResult
                    status="Service Not Found.  The request may have been sent to an invalid URL, or intended for an unsupported operation." xmlns:l7="http://www.layer7tech.com/ws/policy/fault"/>
            </soapenv:Detail>
        </soapenv:Fault>
    </soapenv:Body>

Now I am getting this error 现在我收到了这个错误

Server returned HTTP response code: 500 for URL: https://abc.org/test/ApplicantInformationService

也许你必须在url字符串的末尾添加“?wsdl”:

String url = "https://abc.org/test/ApplicantInformationService?wsdl";

你可以发布你的createSOAPRequest方法代码吗?我认为你的请求没有任何证书问题,因为你从服务器收到一个有效的SOAP响应,看起来你的安全头有问题,检查你的请求SOAP消息的时间戳element,也许您为创建或过期的标签发送了错误的值,服务器拒绝您的请求。

It seems you need to set up the SSL Context for being able to send SOAP over HTTPS in the Java code. 您似乎需要设置SSL上下文,以便能够在Java代码中通过HTTPS发送SOAP。 For doing that you may want to follow this two examples: 为此,您可能需要遵循以下两个示例:

  1. http://blog.hexican.com/2010/12/sending-soap-messages-through-https-using-saaj/ http://blog.hexican.com/2010/12/sending-soap-messages-through-https-using-saaj/
  2. http://ruelajingsantiago.wordpress.com/2013/08/16/saaj-web-service-client-over-ssl/ http://ruelajingsantiago.wordpress.com/2013/08/16/saaj-web-service-client-over-ssl/

Also, if you would consider using a framework for doing this, I would recommend you CXF + Apache Camel: Apache Camel: CXF 另外,如果您考虑使用框架来执行此操作,我建议您使用CXF + Apache Camel: Apache Camel:CXF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM