function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ostateostate 

A Java client code for a custom web service by Apex

Hi, everyone!

I'm trying to access to a web service written in Apex. It's very simple as follwing ;

|  global class MyHello {
|      webService static String getHello(String yourName) {
|          return 'Hello, ' + yourName + '!!';
|      }
|  }

I'm using axis-1.4, and I generate stubs with the following command.

|  java org.apache.axis.wsdl.WSDL2Java -a .\MyHello.xml

where MyHello.xml is gotten from Visualforce.com UI.

Then, I write the following client code ;

|  package com.sforce.soap.client;
|  import com.sforce.soap.schemas._class.MyHello.*;
|
|  public class MyHelloClient {
|      public static void main(String[] args) {
|         try {
|              MyHelloService mhs = new MyHelloServiceLocator();
|              MyHelloPortType mh = mhs.getMyHello();
|              System.out.println("GREETING : " + mh.getHello("FooBar"));
|         }
|          catch(Exception e){
|              e.printStackTrace();
|         }
|      }
|  }

This failes with following exception.

|  AxisFault
|   faultCode: {http://soap.sforce.com/schemas/class/MyHello}INVALID_SESSION_ID
|   faultSubcode:
|   faultString: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session
|   faultActor:
|   faultNode:
|   faultDetail:
|      {http://xml.apache.org/axis/}stackTrace:INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session

"Quick Start" in "Force.com Web Services API Developer's Guide" shows me a sample program to login and set session id.
But I couldn't understand how to implement setting sessionId with the stubs of my custom web service.

If anyone know about this issue, please show me the right way.

thanks.

David VPDavid VP


You'll also need to pull in the regular SFDC WSDL (enterprise or partner), since that's the one where the login method is and where you'll get your sessionId, serverUrl to make calls to etc ... (via the LoginResult element/class). Basically, you still need to generate the classes that you'll need to get the examples that you saw working.
Once you have that sessionId, drop it in the sessionHeader of your requests and you'll be able to call your 'home grown' webservice.

Is this enough to get you going again ?



David


Message Edited by David VP on 09-30-2008 07:42 PM
ostateostate
Hello, and thanks for your reply.

I wrote alternatively as following :

|  package com.sforce.soap.client;

|  import java.io.*;
|  import javax.xml.rpc.ServiceException;
|  import com.sforce.soap.enterprise.LoginResult;
|  import com.sforce.soap.enterprise.SessionHeader;
|  import com.sforce.soap.enterprise.SforceServiceLocator;
|  import com.sforce.soap.enterprise.SoapBindingStub;
|  import com.sforce.soap.enterprise.fault.ExceptionCode;
|  import com.sforce.soap.enterprise.fault.LoginFault;
|  import com.sforce.soap.schemas._class.MyHello.*;

|  public class MyHelloClient {

|      private SoapBindingStub binding;
|      static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
|     
|      public static void main(String[] args) throws ServiceException {
|          MyHelloClient mhc = new MyHelloClient();
|          mhc.run();
|      }
|     
|      private boolean login() throws ServiceException {

|          String userName = getUserInput("Enter username: ");
|          String password = getUserInput("Enter password: ");

|          binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
|          binding.setTimeout(60000);
|          LoginResult loginResult;
|          try {
|              System.out.println("LOGGING IN NOW....");
|              loginResult = binding.login(userName, password);
|          }
|          catch (LoginFault ex) {
|              ExceptionCode exCode = ex.getExceptionCode();
|              if (exCode == ExceptionCode.FUNCTIONALITY_NOT_ENABLED ||
|                  exCode == ExceptionCode.INVALID_CLIENT ||
|                  exCode == ExceptionCode.INVALID_LOGIN ||
|                  exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_DOMAIN ||
|                  exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_TIME ||
|                  exCode == ExceptionCode.ORG_LOCKED ||
|                  exCode == ExceptionCode.PASSWORD_LOCKOUT ||
|                  exCode == ExceptionCode.SERVER_UNAVAILABLE ||
|                  exCode == ExceptionCode.TRIAL_EXPIRED ||
|                  exCode == ExceptionCode.UNSUPPORTED_CLIENT) {
|                  System.out.println("Please be sure that you have a valid username and password.");
|              } else {
|                  System.out.println(ex.getExceptionCode());
|                  System.out.println("An unexpected error has occurred." + ex.getMessage());
|              }
|              return false;
|          } catch (Exception ex) {
|              System.out.println("An unexpected error has occurred: " + ex.getMessage());
|              ex.printStackTrace();
|              return false;
|          }

|          if (loginResult.isPasswordExpired()) {
|              System.out.println("An error has occurred. Your password has expired.");
|              return false;
|          }

|          binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
|          SessionHeader sh = new SessionHeader();
|          sh.setSessionId(loginResult.getSessionId());
|          binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader", sh);

|         
|          MyHelloBindingStub myHello = (MyHelloBindingStub) new MyHelloServiceLocator().getMyHello();
|          myHello._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
|          myHello.setHeader(new MyHelloServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader", sh);       
|          try {
|              System.out.println("Welcome : " + myHello.getHello("FoooBaar"));
|          }
|          catch(Exception e){
|              e.printStackTrace();
|          }
|         
|          return true;
|      }

|      private void run() throws ServiceException {
|          if (login()) {
|              getUserInput("SUCESSFUL LOGIN! Hit the enter key to continue.");
|              System.out.println("SUCCESSFULLY COMPLETED.");
|          }
|      }
|     
|      String getUserInput(String prompt) {
|          System.out.print(prompt);
|          try {
|              return rdr.readLine();
|          }
|          catch (IOException ex) {
|              return null;
|          }
|      }
|     
|     
|  }

But the result is :

|  AxisFault
|   faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
|   faultSubcode:
|   faultString: No operation available for request {http://soap.sforce.com/schemas/class/MyHello}getHello
|   faultActor:
|   faultNode:
|   faultDetail:
|      {http://xml.apache.org/axis/}stackTrace:No operation available for request {http://soap.sforce.com/schemas/class/MyHello}getHello

I can't understand what's wrong with my code.
If you understand, please help me.

Thanks.

SuperfellSuperfell
remove this line
myHello._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
ostateostate
Hi, I got the result!!

|  LOGGING IN NOW....
|  Welcome : Hello, FoooBaar!!
|  SUCESSFUL LOGIN! Hit the enter key to continue.
|  SUCCESSFULLY COMPLETED.

Thanks a lot for your advice!

Alan TangAlan Tang

I had some issue, but after removed this line for endpoint, it doesnot work, i met error "No endpoint".

 

please help~

skrishskrish

I used wsimport to generate the client files but i didn't get nothing like "MyHelloServiceLocator". Could you assist please ?

chercher

what was the username and password that you used to acess the webservice cause I am also having the same problem