java中如何通过soap header传递多个参数?
How to pass multiple parameters through soap header in java?
我是 SOAP 的新手,我正在尝试从 SOAP Web 服务获取一些数据。
我只能从 WS 直接调用一种方法,该方法与从 WSDL 文件生成的方法(端口、代理、服务..)配合使用,但我需要的其他方法需要像这样的身份验证 header :
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthHeader xmlns="http://localhost/webservices/map2">
<UserName>string</UserName>
<Password>string</Password>
<Secure>boolean</Secure>
</AuthHeader>
</soap:Header>
经过一番研究后,我通过使用 SOAPHandler 找到了这个解决方案:
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isRequest) {
try {
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
if (soapHeader == null) {
soapHeader = soapEnv.addHeader();
}
// add a soap headers
QName qnameAuthHeader = new QName("http://localhost/webservices/map2", "AuthHeader");
SOAPHeaderElement soapAuthHeader = soapHeader.addHeaderElement(qnameAuthHeader);
soapAuthHeader.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "UserName"), "user1");
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Password"), "pass1");
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Secure"), "false");
soapMsg.saveChanges();
// tracking
soapMsg.writeTo(System.out);
} catch (SOAPException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
return true;
}
但是当我运行这个
ServiceSoapProxy proxy = new ServiceSoapProxy("http://localhost/ws3.0/service.asmx?wsdl");
ServiceSoap service = proxy.getServiceSoap();
Binding binding = ((BindingProvider) service).getBinding();
我明白了:
Exception in thread "main" java.lang.ClassCastException: localhost.webservices.map2.ServiceSoapStub cannot be cast to javax.xml.ws.BindingProvider
at test.devs.MailiTest.main(MailiTest.java:33)
你可以按照下面的link进行同样的操作,它在实施过程中帮助了我很多。
http://informatictips.blogspot.pt/2013/09/using-message-handler-to-alter-soap.html
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
我也在使用其他方法来读取 soap 网络服务,可能对您有所帮助。
HttpPost httppost = new HttpPost(endPoint);
StringEntity stringentity = new StringEntity(envBody, "UTF-8");
stringentity.setChunked(true);
httppost.setEntity(stringentity);
httppost.addHeader("Accept", "text/xml");
httppost.addHeader("Content-Type", "text/xml");
httppost.addHeader("SOAPAction", soapAction);
String authToken = Base64.encodeBytes("username"
+ ":" + "password".getBytes());
httppost.addHeader("Authorization", "Basic " + authToken);
HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParams = httpclient.getParams();
httpParams.setParameter("http.connection.timeout",
Integer.valueOf(60000));
httpParams.setParameter("http.socket.timeout", Integer.valueOf(60000));
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
在阅读了我自动生成的存根之后,我终于理解并找到了解决我问题的方法。这个答案是为那些正在寻找和我一样的人的。
我刚刚将这一行添加到位于我的 Stub.java:
中的 createCall() 方法中
setHeader("http://localhost/webservices/map2", "AuthHeader", new AuthHeader("username", "password"));
我是 SOAP 的新手,我正在尝试从 SOAP Web 服务获取一些数据。 我只能从 WS 直接调用一种方法,该方法与从 WSDL 文件生成的方法(端口、代理、服务..)配合使用,但我需要的其他方法需要像这样的身份验证 header :
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthHeader xmlns="http://localhost/webservices/map2">
<UserName>string</UserName>
<Password>string</Password>
<Secure>boolean</Secure>
</AuthHeader>
</soap:Header>
经过一番研究后,我通过使用 SOAPHandler 找到了这个解决方案:
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isRequest) {
try {
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
if (soapHeader == null) {
soapHeader = soapEnv.addHeader();
}
// add a soap headers
QName qnameAuthHeader = new QName("http://localhost/webservices/map2", "AuthHeader");
SOAPHeaderElement soapAuthHeader = soapHeader.addHeaderElement(qnameAuthHeader);
soapAuthHeader.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "UserName"), "user1");
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Password"), "pass1");
soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Secure"), "false");
soapMsg.saveChanges();
// tracking
soapMsg.writeTo(System.out);
} catch (SOAPException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
return true;
}
但是当我运行这个
ServiceSoapProxy proxy = new ServiceSoapProxy("http://localhost/ws3.0/service.asmx?wsdl");
ServiceSoap service = proxy.getServiceSoap();
Binding binding = ((BindingProvider) service).getBinding();
我明白了:
Exception in thread "main" java.lang.ClassCastException: localhost.webservices.map2.ServiceSoapStub cannot be cast to javax.xml.ws.BindingProvider
at test.devs.MailiTest.main(MailiTest.java:33)
你可以按照下面的link进行同样的操作,它在实施过程中帮助了我很多。
http://informatictips.blogspot.pt/2013/09/using-message-handler-to-alter-soap.html
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
我也在使用其他方法来读取 soap 网络服务,可能对您有所帮助。
HttpPost httppost = new HttpPost(endPoint);
StringEntity stringentity = new StringEntity(envBody, "UTF-8");
stringentity.setChunked(true);
httppost.setEntity(stringentity);
httppost.addHeader("Accept", "text/xml");
httppost.addHeader("Content-Type", "text/xml");
httppost.addHeader("SOAPAction", soapAction);
String authToken = Base64.encodeBytes("username"
+ ":" + "password".getBytes());
httppost.addHeader("Authorization", "Basic " + authToken);
HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParams = httpclient.getParams();
httpParams.setParameter("http.connection.timeout",
Integer.valueOf(60000));
httpParams.setParameter("http.socket.timeout", Integer.valueOf(60000));
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
在阅读了我自动生成的存根之后,我终于理解并找到了解决我问题的方法。这个答案是为那些正在寻找和我一样的人的。
我刚刚将这一行添加到位于我的 Stub.java:
中的 createCall() 方法中setHeader("http://localhost/webservices/map2", "AuthHeader", new AuthHeader("username", "password"));