代码之家  ›  专栏  ›  技术社区  ›  Ryan

SoapClient的Java版本

  •  1
  • Ryan  · 技术社区  · 11 年前

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

    任何帮助都将不胜感激,因为我已经被困在这件事上一段时间了。

    提前谢谢。

    $param_auth=array(  
     'user'=>$username,
     'id'=>$userID,
     'message'=>$userMessage
    );
    $soapclient = new soapclient(WebsiteAddress);   
    $data->_db = $soapclient->call('uploadMessage',$param_auth);
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   Ryan    10 年前

    最后解决了我的问题(我更改了元素名称等),使用eclipse向您展示了Soap信封和XML响应,我发现这对调试信封很有用)。 希望这能帮助任何试图做类似事情的人。

    “UploadMessage”是“Login”字符串

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

    }