简体   繁体   中英

Call web service method from Java SE program

How can I call a web service method from java desktop client. Basically my java program is running on a desktop which is internet enabled. My web service url is eg. localhost:8090/Service.svc , This web service is basically store data in database we just need to call a web service method and pass a xml string as parameter.

My sample code is look like:

public static void main(String [] args)
    {
        String accessURL,method, parameters[];
        try {

            String xml = "<DocumentElement>"
                        + "<Table1>"
                        + "<ConsumptionID>0</ConsumptionID>"
                        + "<ConsumptionDate>2012/01/01 00:00:00</ConsumptionDate>"
                        + "<MeterNumber>99999901</MeterNumber>"
                        + "<Voltage>200</Voltage>"
                        + "<Ampere>50</Ampere>"
                        + "<PowerFactor>0.91</PowerFactor>"
                        + "<KiloWatt>8525</KiloWatt>"
                        + "<HourValue>3</HourValue>"
                        + "<EndKiloWattHour>5841</EndKiloWattHour>"
                        + "<KVA>1000</KVA>"
                        + "<ClientID>1011</ClientID>"
                        + "<CreatedBy>1</CreatedBy>"
                        + "</Table1>"
                        + "</DocumentElement>";

            accessURL = "http://localhost:8090/Service.svc";
            method = "Save_Consumption";
            parameters = new String[1];

            parameters[0] = xml;
            Service service = new Service();
            Call call = (Call)service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(accessURL));
            call.setOperationName(method);
            Object[] params = new Object[parameters.length];
            params[0]=(Object)(parameters[0]);
            String retval = (String)call.invoke(params);
            System.out.println("Operation Result: " + retval);
        } catch(Exception e) {
            System.err.println(e.toString());
            e.printStackTrace();
        }
    }

But it shows exceptions, here is the stack trace.

run:
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
AxisFault
 faultCode: {http://schemas.microsoft.com/ws/2005/05/addressing/none}ActionNotSupported
 faultSubcode: 
 faultString: The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
 faultActor: 
 faultNode: 
 faultDetail: 
    {http://xml.apache.org/axis/}stackTrace:The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
    at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
    at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
    at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1741)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
    at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
    at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
    at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
    at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
    at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
    at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
    at org.apache.axis.client.Call.invoke(Call.java:2767)
    at org.apache.axis.client.Call.invoke(Call.java:2443)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.lmeasure.utils.HelloClient.main(HelloClient.java:54)

    {http://xml.apache.org/axis/}hostname:eeipldt0132

The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
    at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
    at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
    at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1741)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
    at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
    at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
    at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
    at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
    at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
    at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
    at org.apache.axis.client.Call.invoke(Call.java:2767)
    at org.apache.axis.client.Call.invoke(Call.java:2443)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.lmeasure.utils.HelloClient.main(HelloClient.java:54)

Can anyone know whats wrong with that or provide me any good tutorial/working example to resolve my issue?

The problem seems to be that you are not posting the appropriate XML. For SOAP services it is very difficult to hand-craft SOAP messages. You should let Axis do this for you.

For starters you will the the service WSDL. This is the service definition in XML schema.

Once you have the WSDL, you can generate client stubs. The stubs will allow you to build the client request using Java objects instead of XML.

You do this in Axis using the wsdltojava tool. Here is the documentation: http://axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL .

And here is an example: http://www.digizol.com/2008/07/web-service-axis-tutorial-client-server.html

You can try using the tool wsimport from JDK 6 for getting the client of your WebService. This tool generates the Java Code that you can use in your proyect. For this, you need the WSDL file.

You can try the next command:

 wsimport http://localhost:8090/Service.svc?wsdl -s C:\sources -Xnocompile

And you can use the generate classes for something like that for call to the WebService:

ExampleService exampleService = new ExampleService();
Example example = exampleService.getExamplePort();
ReturnValue rv = example.doSomething(param);

With this way, you not need additional libraries.

You can find a good example in Consuming a Web Service with Java 6 and JAX-WS

It looks like the method name "Save_Consumption" should be set in an HTTP Header with the name "SOAPAction".

I am not familiar with the Service and Call methods, but with the plain old HttpURLConnection API, this would be:

    URL httpUrl = new URL(accessUrl);
    HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
    connection.addRequestProperty("SOAPAction", soapAction);

Like the other posters are saying, you can use the wsimport command, but in my experience, this is a band-aid on a heart attack. Good on you to try and do without!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM