This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Services and the fly likes how to invoke a webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "how to invoke a webservice" Watch "how to invoke a webservice" New topic
Author

how to invoke a webservice

Ajay Chawla
Greenhorn

Joined: Jan 14, 2004
Posts: 11
Hi,
I need to know what needs to be done to invoke a web service? In other words what are the basics for writing a web service client?
Thanks
Ajay
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Only few things,
  • Download Apache Axis
  • Read the documentation on using the pretty-tiny tool WSDL2Java

  • Its a tool which will automatically create the necessary java files (stubs)to for a given webservice(WSDL)
  • compile the java files, you can also import the necessary class to your application
  • run it.


  • Spritle Software Blogs
    Ajay Chawla
    Greenhorn

    Joined: Jan 14, 2004
    Posts: 11
    Thanks Balaji,
    Actually I need to do it without any tool.
    I've to use SOAP API. I think I can do it using Call().
    Can u tell me what I need to specify in Call.setTargetObjectURI()? is it the Web service name or some other identifier?
    Ajay
    Ajay Chawla
    Greenhorn

    Joined: Jan 14, 2004
    Posts: 11
    Iam getting the following fault when i run my client : -
    faultcode :soap:Client
    faultString :Server did not recognize the value of HTTP Header SOAPAction:
    my code is : -
    Call call = new org.apache.soap.rpc.Call();
    URL url = new URL("http://delgtel304:7070/WebService2/JavaService1.asmx");
    call.setTargetObjectURI"http://delgtel304:7070/WebService2/JavaService1.asmx");
    call.setMethodName("JavaTest");
    call.setEncodingStyleURI(org.apache.soap.Constants.NS_URI_SOAP_ENC);
    Vector vParam = new Vector();
    vParam.addElement(new org.apache.soap.rpc.Parameter"str",String.class,"Ajay",null));
    call.setParams(vParam);
    org.apache.soap.rpc.Response resp = call.invoke(url, "");

    if(resp.generatedFault()){
    org.apache.soap.Fault fault = resp.getFault();
    System.out.println("faultcode :"+fault.getFaultCode());
    System.out.println("faultString :"+fault.getFaultString());
    }
    else{
    org.apache.soap.rpc.Parameter result = resp.getReturnValue();
    System.out.println(result.getValue());
    }
    any help is greatly appreciated.
    Thanks
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    Apparently your code is not setting the SOAP-Action header. You can do it with

    [ March 10, 2004: Message edited by: Lasse Koskela ]

    Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    Originally posted by Balaji Loganathan:
    Read the documentation on using the pretty-tiny tool WSDL2Java

    How to get WSDL2Java to work though... Whenever I try I get an error

    C:\work\Java\SOAP\zip>java -cp %AXISCP% org.apache.axis.wsdl.WSDL2Java ZipServic
    e.wsdl
    Exception in thread "main" java.lang.NoClassDefFoundError: javax.wsdl.Definition
    at org.apache.axis.wsdl.toJava.JavaGeneratorFactory.class$(JavaGeneratorFactory.java:163)
    at org.apache.axis.wsdl.toJava.JavaGeneratorFactory.addDefinitionGenerators(JavaGeneratorFactory.java:163)
    at org.apache.axis.wsdl.toJava.JavaGeneratorFactory.addGenerators JavaGeneratorFactory.java:133)
    at org.apache.axis.wsdl.toJava.JavaGeneratorFactory.<init> JavaGeneratorFactory.java:120)
    at org.apache.axis.wsdl.toJava.Emitter.<init>(Emitter.java:120)
    at org.apache.axis.wsdl.WSDL2Java.createParser(WSDL2Java.java:172)
    at org.apache.axis.wsdl.gen.WSDL2.<init>(WSDL2.java:113)
    at org.apache.axis.wsdl.WSDL2Java.<init>(WSDL2Java.java:161)
    at org.apache.axis.wsdl.WSDL2Java.main(WSDL2Java.java:289)

    AXISCP is

    AXISCP=c:\tools\axis-1_1\lib\axis.jar;c:\tools\axis-1_1\lib\commons-discovery.jar;c:\tools\axis-1_1\lib\commons-logging.jar;c:\tools\axis-1_1\lib\log4j-1.2.8.jar;c:\tools\axis-1_1\lib\jaxrpc.jar;c:\tools\axis-1_1\lib\saaj.jar

    added some linebreaks to keep it readable
    other than that the classpath is empty so no conflicting libraries.
    Using J2SDK 1.4.2-b28 (I know Axis has trouble with 1.4 JDKs, but I suppose there must be a workaround?)


    42
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    You need the wsdl4j.jar in your classpath as well (if I remember correctly, that's where the javax.wsdl package is defined).
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    cheers. Sometimes you just don't see your own mistakes however long you stare at them
    Ajay Chawla
    Greenhorn

    Joined: Jan 14, 2004
    Posts: 11
    Hi All,
    Thanks for all those reply's.
    But my problem still remains but now in a different form.
    I modified the code as: -
    System.setProperty("javax.xml.soap.MessageFactory","weblogic.webservice.core.soap.MessageFactoryImpl");
    System.setProperty( "javax.xml.rpc.ServiceFactory","weblogic.webservice.core.rpc.ServiceFactoryImpl");
    ServiceFactory factory = ServiceFactory.newInstance();
    String targetNamespace = "http://tempuri.org/";
    QName serviceName = new QName(targetNamespace,"JavaService1");
    QName portName = new QName(targetNamespace,"JavaService1Soap");
    QName operationName = new QName("http://schemas.xmlsoap.org/soap/http","JavaTest");
    URL wsdlLocation = new URL("http://delgtel304:7070/WebService2/JavaService1.asmx?WSDL");
    Service service = factory.createService(wsdlLocation, serviceName);
    Call call = service.createCall(portName, operationName);
    call.setProperty(javax.xml.rpc.Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
    call.setProperty(javax.xml.rpc.Call.SOAPACTION_URI_PROPERTY, "MySoapAction");
    String result = (String) call.invoke(new Object[] {"BEAS"});
    System.out.println("\n");
    System.out.println("This example shows how to create a dynamic client application that invokes a non-WebLogic Web service.");
    System.out.println("The webservice used was:http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl");
    System.out.println("The quote for BEAS is: ");
    System.out.println(result);

    I get this on console :-
    WARNINIG: Unable to find a javaType for the xmlType:['http://tempuri.org/']:Java
    Test. Make sure that you have registered this xml type in the type mapping
    Using SOAPElement instead
    WARNINIG: Unable to find a javaType for the xmlType:['http://tempuri.org/']:Java
    TestResponse. Make sure that you have registered this xml type in the type mappi
    ng
    Using SOAPElement instead
    Exception in thread "main" javax.xml.rpc.JAXRPCException: failed to invoke opera
    tion. Error in the soap layer (jaxm); nested exception is: Message[ failed to se
    rialize xml:weblogic.xml.schema.binding.SerializationException: mapping lookup f
    ailure. class=interface javax.xml.soap.SOAPElement class context=TypedClassConte
    xt{schemaType=['http://tempuri.org/']:JavaTest}]StackTrace[
    javax.xml.soap.SOAPException: failed to serialize xml:weblogic.xml.schema.bindi
    ng.SerializationException: mapping lookup failure. class=interface javax.xml.soa
    p.SOAPElement class context=TypedClassContext{schemaType=['http://tempuri.org/']
    :JavaTest}
    at weblogic.webservice.core.DefaultPart.toXML(DefaultPart.java:266)
    at weblogic.webservice.core.DefaultMessage.toXML(DefaultMessage.java:456
    )
    at weblogic.webservice.core.DefaultOperation.invoke(DefaultOperation.jav
    a:407)
    at weblogic.webservice.core.DefaultOperation.invoke(DefaultOperation.jav
    a:363)
    at weblogic.webservice.core.rpc.CallImpl.invoke(CallImpl.java:423)
    at weblogic.webservice.core.rpc.CallImpl.invoke(CallImpl.java:292)
    at TestClient2.main(TestClient2.java:35)
    Caused by: weblogic.xml.schema.binding.SerializationException: mapping lookup fa
    ilure. class=interface javax.xml.soap.SOAPElement class context=TypedClassContex
    t{schemaType=['http://tempuri.org/']:JavaTest}
    at weblogic.xml.schema.binding.RuntimeUtils.lookup_serializer(RuntimeUti
    ls.java:144)
    at weblogic.xml.schema.binding.RuntimeUtils.invoke_serializer(RuntimeUti
    ls.java:180)
    at weblogic.xml.schema.binding.RuntimeUtils.invoke_serializer(RuntimeUti
    ls.java:167)
    at weblogic.webservice.core.DefaultPart.toXML(DefaultPart.java:258)
    ... 6 more
    ]
    at weblogic.webservice.core.rpc.CallImpl.invoke(CallImpl.java:432)
    at weblogic.webservice.core.rpc.CallImpl.invoke(CallImpl.java:292)
    at TestClient2.main(TestClient2.java:35)
    Thanks
    Ajay
    Vishwa Kumba
    Ranch Hand

    Joined: Aug 27, 2003
    Posts: 1064
    Did anybody solve this problem?. I am getting a similar error in Weblogic 8.1, when trying to invoke a web service with JAX-RPC (dynamic wsdl method).
    Niral Trivedi
    Ranch Hand

    Joined: Nov 26, 2001
    Posts: 46
    Hi,

    I am also having similar problem when trying to connect to a WS using Axis client.

    Can somebody tell me what should be the value for
    ? I tried setting it to blank and some other value but I get this error regardless. What is the significance of this property?

    Thanks
     
    Consider Paul's rocket mass heater.
     
    subject: how to invoke a webservice
     
    Similar Threads
    Webshere Integration Developer issues
    Invoking a https webservice
    webservices swings
    How to send xml file to a different web server
    How is web service compostion implemented programatically?