1

我在 PhP 中有一个工作的 soaprequest,我正在尝试创建一个需要相同调用的 java 程序,但是我真的很难找到在 java 中创建以下 php 代码的方法,我找到了许多解释肥皂请求的网站java 但我似乎不知道如何发送 $param_auth 数组。

任何帮助将不胜感激,因为我已经坚持了一段时间。

提前致谢。

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

1 回答 1

1

终于解决了我的问题(我更改了元素名称等),使用 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>

创建上述信封的代码如下:-

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

}

于 2013-10-29T09:38:45.157 回答