2011-04-04 95 views
7

我有一個webservice我想建立一個客戶端。肥皂ui生成的代碼

我有以下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> 

我可以運行在肥皂的用戶界面,請求和取回響應的認證是成功的。

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

回答

3

與您的代碼的問題是缺乏SOAP頭認證要素。看看的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> 

您服務器失敗,因爲不正確的XML的異常,當有在CheckIfAuthorized請求沒有身份驗證的元素。以下是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() 
+0

@grantk是否解決了我們的問題? – tgkprog 2013-07-13 04:02:31