A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© BlackHorse001 中级黑马   /  2015-9-5 08:49  /  306 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

java调用webservice

有一些已有的webservice服务,由xfire生成发布,有些有参数,有些无参数,无参数的直接我直接使用org.codehaus.xfire这个包里的Client来动态生成客户端。然后调用就可以了。非常简单

Client client = null;
        try {
            client = new Client(
                    new URL(
                            “http://leaver.me/testService?wsdl“));
            client.invoke(“refreshAllCache”, new Object[0]);

    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (Exception e) {

        e.printStackTrace();
    }</pre>

但对于有参的,且是服务器自定义的类作为参数的时候,实在是搞不定。。不管是把自定义的类放到本地,包名一致,在invoke的时候生成这个对象还是其他什么方法。都无法完成。

最终换了直接发送soap报文来完成。dirty hack啊。如果你有一些好的方法希望不吝赐教。

解决方案来源自stackoverflow,因为stackoverflow现在国内好像有时候打不开。因此把代码贴过来。有疑问的话留言讨论。

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

public class SOAPClientSAAJ {

/**
* Starting point for the SAAJ - SOAP Client Testing
*/
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 = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
        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 {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "http://ws.cdyne.com/";

    //(www.111cn.net) SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("example", serverURI);

    /*
    Constructed SOAP Request Message:
    &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/"&gt;
        &lt;SOAP-ENV:Header/&gt;
        &lt;SOAP-ENV:Body&gt;
            &lt;example:VerifyEmail&gt;
                &lt;example:email&gt;mutantninja@gmail.com&lt;/example:email&gt;
                &lt;example:LicenseKey&gt;123&lt;/example:LicenseKey&gt;
            &lt;/example:VerifyEmail&gt;
        &lt;/SOAP-ENV:Body&gt;
    &lt;/SOAP-ENV:Envelope&gt;
     */

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
    soapBodyElem1.addTextNode("mutantninja@gmail.com");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
    soapBodyElem2.addTextNode("123");

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

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

}

from:http://www.111cn.net/jsp/Java/77784.htm

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马