肥皂用户界面生成的代码

发布于 2024-10-30 07:46:16 字数 6168 浏览 0 评论 0原文

我有一个网络服务,我正在尝试为其构建客户端。

我有以下 wsdl:

http://www.cmicdataservices.com/datacenter/service.asmx?wsdl

它需要身份验证。查看 WSDL 描述,我发现没有任何方法采用身份验证对象,也没有将用户名和密码作为参数。我使用 Netbeans 为 WSDL 生成了 jax-ws 源。然而我不知道之后该做什么。

使用soapui,我可以连接到网络服务并运行所有方法。但我想再次将其构建到一个无需我交互即可运行的客户端中。

我的问题在于弄清楚如何使用这个生成的代码,netbeans.tv 似乎有一个视频(netbeans SOAUI 插件视频 2),但现已丢失。有谁知道任何教程或知道如何使用此生成的代码访问网络服务的任何示例?

所以我有一个方法 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\"?>\r\n");
            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'>\r\n");

            //Soap request header start
            wout.write("<soap12:Header>\r\n");

            //Start writing soap request - Authentication
            wout.write("<Authentication xmlns=");
            wout.write("'http://www.cmicdataservices.com/'>\r\n");
            wout.write("<UserName>" + UserName + "</UserName>\r\n");
            wout.write("<Password>" + Password + "</Password>\r\n");
            // End Authentication
            wout.write("</Authentication>\r\n");

            //End the header
            wout.write("</soap12:Header>\r\n");

            //Start writing the body
            wout.write("<soap12:Body>");
            wout.write("<GetCurrentDataVer1 xmlns=");
            wout.write("'http://www.cmicdataservices.com/' />\r\n");
            // End the Body
            wout.write("</soap12:Body>\r\n");

            // End the Envelope
            wout.write("</soap12:Envelope>\r\n");

            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);
         }
     }

I have a webservice I am trying to build a client for.

I have the following wsdl:

http://www.cmicdataservices.com/datacenter/service.asmx?wsdl

It requires authentication. Looking at the WSDL description I see no method that takes an authentication object, nor username and passwords as arguments. Using Netbeans I have generated jax-ws sources for the WSDL. I however can not figure out what to do after that.

Using soapui I can connect to the webservice and run all the methods. But once again, I want to build this into a client that can be run without my interaction.

My problem comes in figuring out how to use this generated code, which it appears netbeans.tv had a video(netbeans soapui plugin video 2) which has since been lost. Does anyone know of any tutorials or know of any examples of how I can use this generated code to access the webservice?

so I have a method CheckifAuthorized()

Running in soapui I get the following 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>

I can then run that request in soap ui and get back the response that authentication was a success.

With the jax-ws code generated with netbeans and with soapui as well I have the following:

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();
    }
}

This will fail with the following error

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

This is the same problem I ran into when trying to use python for the service. I have since chosen to go with Java as I feel I may have quicker turnaround on parsing the xml and creating objects as I already have the entities for this created.

Thank you.

Grant

I did not want to answer this because I would still like to figure out what I can do here, but I did just end up writing the request by hand with the following. Now I can just convert this into an xml object and go about my way, but I imagine soapui makes all of this much easier. What I really do not understand is how to use soapui to build this request and incorporate it into my project:

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\"?>\r\n");
            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'>\r\n");

            //Soap request header start
            wout.write("<soap12:Header>\r\n");

            //Start writing soap request - Authentication
            wout.write("<Authentication xmlns=");
            wout.write("'http://www.cmicdataservices.com/'>\r\n");
            wout.write("<UserName>" + UserName + "</UserName>\r\n");
            wout.write("<Password>" + Password + "</Password>\r\n");
            // End Authentication
            wout.write("</Authentication>\r\n");

            //End the header
            wout.write("</soap12:Header>\r\n");

            //Start writing the body
            wout.write("<soap12:Body>");
            wout.write("<GetCurrentDataVer1 xmlns=");
            wout.write("'http://www.cmicdataservices.com/' />\r\n");
            // End the Body
            wout.write("</soap12:Body>\r\n");

            // End the Envelope
            wout.write("</soap12:Envelope>\r\n");

            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);
         }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-11-06 07:46:16

您的代码的问题是 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()

The problem with your code is lack of Authentication element in the SOAP header. Take a look at the WSDL, it should always be there:

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

You server fails with exception because of incorrect XML when there is no Authentication element in the CheckIfAuthorized request. Here is sample client in Python to demonstrate the idea of solving the problem, I think it's not a problem for you to convert it to 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文