2013-10-18 61 views
1

我在PhP中有一個可用的soaprequest,我試圖創建一個需要相同調用的java程序,但是我真的非常想找到在java中創建下面的php代碼的方式,我有在java中找到了解釋soap請求的numerus網站,但我似乎無法如何發送$ param_auth數組。SoapClient的Java版本

任何幫助將不勝感激,因爲我一直堅持這一段時間。

在此先感謝。

$param_auth=array( 
'user'=>$username, 
'id'=>$userID, 
'message'=>$userMessage 
); 
$soapclient = new soapclient(WebsiteAddress); 
$data->_db = $soapclient->call('uploadMessage',$param_auth); 

回答

0

終於解決了我的問題(IVE改變元素名稱等),使用Eclipse中顯示了SOAP信封和XML響應,我發現在調試信封有用)。 希望這可以幫助任何人嘗試做類似的事情。

的「UploadMessage」是「登錄」串和

$param_auth=array( 
'user'=>$username, 
'id'=>$userID, 
'message'=>$userMessage 
); 

是用戶名和密碼,(留出了消息的一部分)。

此代碼發送一個信封這樣的服務器: -

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:Login="Your URL"><SOAP-ENV:Header/><SOAP-ENV:Body><Login><UserName>YourUserName</UserName><Password>YourPassword</Password></Login></SOAP-ENV:Body></SOAP-ENV:Envelope> 

代碼來創建上述Envelope是如下: -

import javax.xml.soap.*; 
import javax.xml.transform.*; 
import javax.xml.transform.stream.*; 

public class SoapCall { 

public static void main(String args[]) { 
    try { 
     // Create SOAP Connection 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

     // Send SOAP Message to SOAP Server. 
     String url = "Your URL"; 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 

     // Process the SOAP Response 
     printSOAPResponse(soapResponse); 


     soapConnection.close(); 
    } catch (Exception e) { 
     System.err.println("Error occurred while sending SOAP Request to Server"); 
     e.printStackTrace(); 
    } 
} 

private static SOAPMessage createSOAPRequest() throws Exception { 

    String YourUsername = "UserName"; 
    String YourPassword = "Password"; 

    MessageFactory messageFactory = MessageFactory.newInstance(); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 
    SOAPPart soapPart = soapMessage.getSOAPPart(); 

    String serverURI = "Your URL"; 

    // SOAP Envelope 
    SOAPEnvelope envelope = soapPart.getEnvelope(); 
    envelope.addNamespaceDeclaration("Login", serverURI); 


    SOAPBody soapBody = envelope.getBody(); 

    SOAPElement soapBodyElem = soapBody.addChildElement("Login"); 

    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("UserName"); 
    soapBodyElem2.addTextNode(YourUserName); 
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Password"); 
    soapBodyElem3.addTextNode(YourPassword); 

    MimeHeaders headers = soapMessage.getMimeHeaders(); 
    headers.addHeader("SOAPAction", serverURI + "Login"); 

    soapMessage.saveChanges(); 

    /* Print the request message */ 
    System.out.print("Request SOAP Message = "); 
    soapMessage.writeTo(System.out); 
    System.out.println(); 

    return soapMessage; 
} 

/** 
* Method used to print the SOAP Response 
*/ 
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    Source sourceContent = soapResponse.getSOAPPart().getContent(); 
    System.out.print("\nResponse SOAP Message = "); 
    StreamResult result = new StreamResult(System.out); 
    transformer.transform(sourceContent, result); 
} 

}