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
ShekharShekhar 

Need help to connect to salesforce

I am new to salesforce. I am trying to log in to the salesforce server using Java. I read the Webservices API page and the cookbook and was able to write the following code. I used the samples given on the website.
Code:
package org.salesforce.samples;

import java.io.BufferedReader;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.sforce.soap.partner.LoginResult;
import com.sforce.soap.partner.SessionHeader;
import com.sforce.soap.partner.SforceServiceLocator;
import com.sforce.soap.partner.SoapBindingStub;
import com.sforce.soap.partner.fault.ApiFault;
import com.sforce.soap.partner.fault.LoginFault;

public class PartnerSamples {

 private SoapBindingStub binding;
 private LoginResult loginResult = null;
 private String un = "";
 private String  pw = "";
  
        static BufferedReader rdr = new BufferedReader(new java.io.InputStreamReader(System.in));

 public PartnerSamples() {
 }

 public static void main(String[] args) {
  PartnerSamples samples1 = new PartnerSamples();
  samples1.login();
 }

 String getUserInput(String prompt) {
  System.out.print(prompt);
  try {
   return rdr.readLine();
  } catch (IOException ex) {
                    System.out.println(ex);
  return null;
  }
 }
 private boolean login() {
  un = getUserInput("Enter user name: ");
  if (un.length() == 0) {
   return false;
  }
  pw = getUserInput("Enter password: ");
  if (pw.length() == 0) {
   return false;
  }
  System.out.println("Creating the binding to the web service...");
         try {
   binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
  } catch (ServiceException ex) {
   System.out.println("ERROR: createing binding to soap service, error was: \n"+ ex.getMessage());
   System.out.print("Hit return to continue...");
   return false;
  }
  binding.setTimeout(60000);
  System.out.println("LOGGING IN NOW....");
  try {
   loginResult = binding.login(un, pw);
  }catch (LoginFault lf) {
   System.out.println("in 1");
   lf.printStackTrace();
   getUserInput("\nHit return to continue...");
   return false;
  } catch (ApiFault af) {
   System.out.println("in 2");
   getUserInput("\nHit return to continue...");
  } catch (RemoteException re) {
   System.out.println("in 3");
   re.printStackTrace();
   getUserInput("\nHit return to continue...");
   return false;
  }


  System.out.println("\nThe session id is: " + loginResult.getSessionId());
  System.out.println("\nThe new server url is: " + loginResult.getServerUrl());
  SessionHeader sh = new SessionHeader();
  sh.setSessionId(loginResult.getSessionId());
  binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
  return true;
 }
}
I am successfully able to build the code and run it. However when trying to login, i got the java.net.connectException. The complete exception is below.
Code:
init:
deps-jar:
compile:
run:
Enter user name:abc@xyz.com
Enter password:password
Creating the binding to the web service...
LOGGING IN NOW....
in 3
AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode: 
 faultString: java.net.ConnectException: Connection timed out: connect
 faultActor: 
 faultNode: 
 faultDetail: 
        {http://xml.apache.org/axis/}stackTrace:java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:519)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:353)
        at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:71)
        at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:92)
        at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)
        at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)
        at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)
        at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
        at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
        at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
        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.sforce.soap.partner.SoapBindingStub.login(SoapBindingStub.java:1844)
        at org.salesforce.samples.PartnerSamples.login(PartnerSamples.java:73)
        at org.salesforce.samples.PartnerSamples.main(PartnerSamples.java:33)

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

java.net.ConnectException: Connection timed out: connect
        at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
        at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:154)
        at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
        at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
        at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
        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.sforce.soap.partner.SoapBindingStub.login(SoapBindingStub.java:1844)
        at org.salesforce.samples.PartnerSamples.login(PartnerSamples.java:73)
        at org.salesforce.samples.PartnerSamples.main(PartnerSamples.java:33)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:519)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:353)
        at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:71)
        at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:92)
        at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)

        at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)
        at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)
        ... 12 more
Hit return to continue...
 
I am using Java 1.6 and have just added the jar files for axis on the path. I am not running axis or tomcat in the background as i think i am trying to connect to the server on salesforce so i do not need to. Am i right in assuming this? The error is  caught in third catch block which is for remote exception. I am not able to understand what can i do to make it work.  Thank you in advance and sorry for the long post.
 
Shekhar
 
 
 


 
werewolfwerewolf
You're not setting your endpoint URL, which in your case would be:
binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
just after login.  The sample on this page is actually probably the simplest to follow.
kamalkamal
Also check if there is any firewall restriction of ur company. I faced the same problem when i start using the salesforce.com initially
crocodilecrocodile
Hi Shekhar,
                     Even i got the same exceptions while Logon to Salesforce site. Please guide me clearly the way how you rectified this error and also guide me how to set the proxy settings.

Thanks in adv,
-Vissu