在 Java 和 SOAPUI 中使用 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20210993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consuming a web service in java and SOAPUI
提问by yogsma
I am consuming a webservice in java. This service is using UserNameToken, Timestamp and Signature for message level security. I have been given SoapUI project file(xml) to try and get the data from the service. Everything works fine in SoapUI.
我正在使用 java 中的 web 服务。此服务使用 UserNameToken、Timestamp 和 Signature 来确保消息级别的安全性。我已获得 SoapUI 项目文件 (xml) 以尝试从服务中获取数据。在 SoapUI 中一切正常。
Now when I am trying to use the same soapUI file to generate the artifacts, I am getting a message "Receiver Policy falsified"
. Why is that I can connect to the service in soapUI, but I can not in java?
现在,当我尝试使用相同的 soapUI 文件来生成工件时,我收到一条消息"Receiver Policy falsified"
。为什么我可以在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. I have done similar setting in SOAP UI in SSL settings and it works there.
所以我发现我没有使用传输层安全发送密钥库。我如何在 SOAP 请求中发送它。我在 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 代码是这样的
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
回答by ozkxr
Maybe you have to append "?wsdl" at the end of the url string:
也许您必须在 url 字符串的末尾附加“?wsdl”:
String url = "https://abc.org/test/ApplicantInformationService?wsdl";
回答by vzamanillo
Could you please post your createSOAPRequest method code?, I think you have not any certificate problems with the request because you are receiving a valid SOAP response from server, looks like something is wrong with your security headers, check your request SOAP message for the Timestamp element, maybe you are sending an incorrect value for created or expires tags and the server rejects your request.
您能否发布您的 createSOAPRequest 方法代码?,我认为您的请求没有任何证书问题,因为您从服务器接收到有效的 SOAP 响应,看起来您的安全标头有问题,请检查您的请求 SOAP 消息以获取时间戳元素,也许您为 created 或 expires 标签发送了不正确的值,并且服务器拒绝了您的请求。
回答by hveiga
It seems you need to set up the SSL Context for being able to send SOAP over HTTPS in the Java code. For doing that you may want to follow this two examples:
似乎您需要设置 SSL 上下文才能在 Java 代码中通过 HTTPS 发送 SOAP。为此,您可能需要遵循以下两个示例:
- http://blog.hexican.com/2010/12/sending-soap-messages-through-https-using-saaj/
- http://ruelajingsantiago.wordpress.com/2013/08/16/saaj-web-service-client-over-ssl/
- http://blog.hexican.com/2010/12/sending-soap-messages-through-https-using-saaj/
- 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