You need to sign in to do that
Don't have an account?
Problem Calling Apex WebService from Java.
I feel I'm almost there :)
The code below connects to the Salesforce server but ultimately fails with a 'no endpoint' exception.
I think I'm just not passing on the protocol credentials such as service and url endpoint to the object making the call, but
I'm only guessing.
Also are there any pieces of sample code that make a WebService call from Java as I've not been able to find any?
Can anyone help me please?
import com.sforce.soap.schemas._class.MyWebService.*;
import java.net.*;
import com.sforce.soap.enterprise.*;
import com.sforce.soap.enterprise.fault.LoginFault;
import com.sforce.soap.enterprise.fault.ExceptionCode;
public class TestCall{
public static void main(String[] args){
LoginResult loginResult;
System.out.println("start of program");
try{
SoapBindingStub bind= (SoapBindingStub) new SforceServiceLocator().getSoap();
System.out.println("after call to getSoap()");
bind.setTimeout(60000);
loginResult = bind.login("myadminuser", "mypassword");
System.out.println("straight after login call");
bind._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
com.sforce.soap.enterprise.SessionHeader sh = new com.sforce.soap.enterprise.SessionHeader();
sh.setSessionId(loginResult.getSessionId());
bind.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),
"SessionHeader", sh);
MyWebServiceBindingStub stub = new MyWebServiceBindingStub();
System.out.println("about to make call");
stub.myMethod();
}
catch (LoginFault ex) {
// The LoginFault derives from AxisFault
ExceptionCode exCode = ex.getExceptionCode();
System.out.println("######" + exCode);
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 {
// Write the fault code to the console
System.out.println(ex.getExceptionCode());
// Write the fault message to the console
System.out.println("An unexpected error has occurred." + ex.getMessage());
}
}
catch(Exception e){
System.out.println("error" + e.getMessage());
}
}
}
Success!! The following code sucessfully calls an Apex Web Service from Java. The System log indicates the call is being
made. I am sure there is a certain amount of duplication of operations in this code and it needs cleaning up. But it does work! :smileyhappy:
import com.sforce.soap.schemas._class.MyWebService.*;
import java.net.*;
import com.sforce.soap.enterprise.*;
import com.sforce.soap.enterprise.fault.LoginFault;
import com.sforce.soap.enterprise.fault.ExceptionCode;
public class TestCall{
public static void main(String[] args){
LoginResult loginResult;
System.out.println("start of program");
try{
SoapBindingStub bind= (SoapBindingStub) new SforceServiceLocator().getSoap();
System.out.println("after call to getSoap()");
bind.setTimeout(60000);
loginResult = bind.login("adminusername", "adminpassword");
System.out.println("straight after login call");
bind._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
com.sforce.soap.enterprise.SessionHeader sh = new com.sforce.soap.enterprise.SessionHeader();
sh.setSessionId(loginResult.getSessionId());
bind.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),
"SessionHeader", sh);
MyWebServiceServiceLocator locator = new MyWebServiceServiceLocator();
//************ new stuff here.
//ApexWebServiceTestService svc = new ApexWebServiceTestServiceLocator();
java.net.URL url = new java.net.URL(locator.getMyWebServiceAddress());
MyWebServiceBindingStub stub = new MyWebServiceBindingStub(url, locator);
//SessionHeader sh = new SessionHeader();
//sh.setSessionId(sessionId);
String sforceURI = locator.getMyWebServiceAddress();
stub.setHeader(sforceURI, "SessionHeader", sh);
// ***********end of new stuff
System.out.println("about to make call");
stub.myMethod();
}
catch (LoginFault ex) {
// The LoginFault derives from AxisFault
ExceptionCode exCode = ex.getExceptionCode();
System.out.println("######" + exCode);
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 {
// Write the fault code to the console
System.out.println(ex.getExceptionCode());
// Write the fault message to the console
System.out.println("An unexpected error has occurred." + ex.getMessage());
}
}
catch(Exception e){
System.out.println("error" + e.getMessage());
}
}
}