肥皂ui生成的代码
我有一个web服务,我正在尝试构建一个客户端。
我有以下wsdl:
http://www.cmicdataservices.com/datacenter/service.asmx?wsdl
它需要认证。 查看WSDL描述,我看不到任何采用身份验证对象的方法,也不会将用户名和密码作为参数。 使用Netbeans我已经为WSDL生成了jax-ws源代码。 然而,我不知道在那之后要做什么。
使用soapui我可以连接到webservice并运行所有的方法。 但是,我想再次将其构建到可以在没有我的交互的情况下运行的客户端。
我的问题来解决如何使用这个生成的代码,它看起来netbeans.tv有一个视频(netbeans soapui插件视频2),这已经失去了。 有谁知道任何教程或知道我如何使用此生成的代码访问Web服务的任何示例?
所以我有一个方法CheckifAuthorized()
在soapui中运行我得到以下xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:cmic="http://www.cmicdataservices.com/">
<soap:Header>
<cmic:Authentication>
<!--Optional:-->
<cmic:UserName>username</cmic:UserName>
<!--Optional:-->
<cmic:Password>password</cmic:Password>
</cmic:Authentication>
</soap:Header>
<soap:Body>
<cmic:CheckIfAuthorized/>
</soap:Body>
</soap:Envelope>
然后,我可以在soap ui中运行该请求并获取身份验证成功的响应。
使用netbeans和soapui生成的jax-ws代码以及我有以下几点:
package javaapplication7;
/**
*
* @author grant
*/
public class Main {
public static void main(String[] args) {
Boolean result = checkIfAuthorized();
System.out.println("The result is: " + result);
}
private static boolean checkIfAuthorized() {
javaapplication7.CMICDatacenterService service = new javaapplication7.CMICDatacenterService();
javaapplication7.CMICDatacenterServiceSoap port = service.getCMICDatacenterServiceSoap();
return port.checkIfAuthorized();
}
}
这将失败并出现以下错误
run:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Server was unable to process request. ---> Object reference not set to an instance of an object.
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
at $Proxy30.checkIfAuthorized(Unknown Source)
at javaapplication7.Main.checkIfAuthorized(Main.java:24)
at javaapplication7.Main.main(Main.java:17)
Java Result: 1
这与我尝试使用python进行服务时遇到的问题相同。 我自从选择使用Java,因为我觉得我可能在解析xml和创建对象方面有更快的周转时间,因为我已经为此创建了实体。
谢谢。
格兰特
我不想回答这个问题,因为我仍然想知道我可以在这里做什么,但是我最终只能用下面的方式写下请求。 现在我可以将它转换成一个xml对象并按照我的方式进行操作,但是我认为soapui使得这一切变得更加简单。 我真的不明白的是如何使用soapui来构建这个请求并将其合并到我的项目中:
public class Main {
public final static String DEFAULT_SERVER =
"http://www.cmicdataservices.com/datacenter/service.asmx";
public final static String SOAP_ACTION =
"http://www.cmicdataservices.com/CheckIfAuthorized";
public static void main(String[] args) {
String server = DEFAULT_SERVER;
String UserName = "Username";
String Password="Password";
try{
URL url = new URL(server);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
connection.setRequestProperty("Host", "www.cmicdataservices.com");
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
// Uncomment the following and comment out the previous two lines to see your xml
//BufferedWriter wout = new BufferedWriter(new FileWriter("/tmp/testXML.xml"));
//Start writing soap request - Envelope
wout.write("<?xml version="1.0" encoding="utf-8"?>rn");
wout.write("<soap12:Envelope ");
wout.write("xmlns:xsi=");
wout.write("'http://www.w3.org/2001/XMLSchema-instance' ");
wout.write("xmlns:xsd=");
wout.write("'http://www.w3.org/2001/XMLSchema' ");
wout.write("xmlns:soap12=");
wout.write("'http://www.w3.org/2003/05/soap-envelope'>rn");
//Soap request header start
wout.write("<soap12:Header>rn");
//Start writing soap request - Authentication
wout.write("<Authentication xmlns=");
wout.write("'http://www.cmicdataservices.com/'>rn");
wout.write("<UserName>" + UserName + "</UserName>rn");
wout.write("<Password>" + Password + "</Password>rn");
// End Authentication
wout.write("</Authentication>rn");
//End the header
wout.write("</soap12:Header>rn");
//Start writing the body
wout.write("<soap12:Body>");
wout.write("<GetCurrentDataVer1 xmlns=");
wout.write("'http://www.cmicdataservices.com/' />rn");
// End the Body
wout.write("</soap12:Body>rn");
// End the Envelope
wout.write("</soap12:Envelope>rn");
wout.flush();
wout.close();
//BufferedWriter fout = new BufferedWriter(new FileWriter("/tmp/testXMLResponse.xml"));
InputStream in = connection.getInputStream();
createFile(in, "/tmp/testXMLResponse.xml");
}
catch (IOException e) {
System.err.println(e);
}
}
public static void createFile(InputStream io, String fileName) throws IOException {
FileOutputStream fout = new FileOutputStream(fileName);
byte[] buf = new byte[256];
int read = 0;
while ((read = io.read(buf)) != -1){
fout.write(buf, 0, read);
}
}
您的代码存在的问题是SOAP标头中缺少Authentication元素。 看看WSDL,它应该始终在那里:
<wsdl:operation name="CheckIfAuthorized">
<soap:operation soapAction="http://www.cmicdataservices.com/CheckIfAuthorized" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
<soap:header message="tns:CheckIfAuthorizedAuthentication" part="Authentication" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
当CheckIfAuthorized请求中没有Authentication元素时,您的服务器因异常XML而异常失败。 以下是Python中的示例客户端,用于演示解决问题的想法,我认为将其转换为Java不是问题。
from suds.client import Client
client = Client("http://www.cmicdataservices.com/datacenter/service.asmx?wsdl")
auth = client.factory.create('Authentication')
auth.UserName = "username"
auth.Password = "password"
client.set_options(soapheaders=auth)
client.service.CheckIfAuthorized()
链接地址: http://www.djcxy.com/p/6013.html
下一篇: Shortest path in Directed Acyclic Graph with small degree