soap的client端JAVA例子

(http://stackoverflow.com/questions/15948927/working-soap-client-example)


I'm trying to find a simple (ha) SOAP example in JAVA with a working service any I seem to be finding are not working.

I have tried this one from this example but it's just not working, it's asking me to put a forward slash in but it's in there and nothing happening.

So does anyone know any SOAP example links, I can download/request and mess with?

Thanks for your help.

-------------------------------------------------------------------------


To implement simple SOAP clients in Java, you can use the SAAJ framework (it is shipped with JSE 1.6 and above):

SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.

See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    public static void main(String args[]) throws Exception {
        // 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);

        // print SOAP Response
        System.out.print("Response SOAP Message:");
        soapResponse.writeTo(System.out);

        soapConnection.close();
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

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

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

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

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

}
Got this error <faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring> –  M_K  Apr 11 '13 at 13:39
 
The SOAPAction HTTP header was missing. Corrected it by adding the lines MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", "http://ws.cdyne.com/VerifyEmail");. I now think it's strange it worked before without it. :) –  acdcjunior  Apr 11 '13 at 14:01
2 
thanks,good example –  janwen  Jun 26 '13 at 7:16
3 
very helpful for starting to learn soap! –  mel3kings  Jul 17 '13 at 2:47
5 
It took me forever to find a simple SOAP java example - thanks for the great example! –  Jarrett  Feb 27 '14 at 15:22

Yes, if you can acquire any WSDL file, then you can use SoapUI to create mock service of that service complete with unit test requests. I created an example of this (using Maven) that you can try out.

Also, here is a giant list of free webservices someone posted.


 
Got this error <faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring> –  M_K  Apr 11 '13 at 13:39
 
The SOAPAction HTTP header was missing. Corrected it by adding the lines MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", "http://ws.cdyne.com/VerifyEmail");. I now think it's strange it worked before without it. :) –  acdcjunior  Apr 11 '13 at 14:01
2 
thanks,good example –  janwen  Jun 26 '13 at 7:16
3 
very helpful for starting to learn soap! –  mel3kings  Jul 17 '13 at 2:47
5 
It took me forever to find a simple SOAP java example - thanks for the great example! –  Jarrett  Feb 27 '14 at 15:22


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值