使用Web服务连接器从java访问salesforce

时间:2013-02-08 10:47:13

标签: java web-services soap salesforce

我正在尝试从java类(在我的本地计算机上)连接到Salesforce。我使用了WSC-22.jar(webservice连接器)并在eclipse中使用了相同的库。我还将企业wsdl解析为jar并在eclipse中上传了库。我正在运行下面的错误输出的java类。

package wsc;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class main {

static final String USERNAME = "username";
static final String PASSWORD = "password + sec token";
static EnterpriseConnection connection;

public static void main(String[] args) {

ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
//config.setTraceMessage(true);

try {

  connection = com.sforce.soap.enterprise.Connector.newConnection(config);

  // display some current settings
  System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
  System.out.println("Service EndPoint: "+config.getServiceEndpoint());
  System.out.println("Username: "+config.getUsername());
  System.out.println("SessionId: "+config.getSessionId());

  // run the different examples
  queryContacts();
  createAccounts();
  updateAccounts();
  deleteAccounts();


} catch (ConnectionException e1) {
    System.out.println("hello world");
    e1.printStackTrace();
}  

}
//somemore code----

错误消息:     com.sforce.ws.ConnectionException:无法向

发送请求
https://login.salesforce.com/services/Soap/c/26.0
    at com.sforce.ws.transport.SoapConnection.send(SoapConnection.java:120)
    at com.sforce.soap.enterprise.EnterpriseConnection.login(EnterpriseConnection.java:1)
    at com.sforce.soap.enterprise.EnterpriseConnection.<init>(EnterpriseConnection.java:1)
    at com.sforce.soap.enterprise.Connector.newConnection(Connector.java:1)
    at wsc.main.main(main.java:32)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)hello world

    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:570)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:411)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:525)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:966)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1031)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at com.sforce.ws.transport.JdkHttpTransport.connectRaw(JdkHttpTransport.java:133)
at com.sforce.ws.transport.JdkHttpTransport.connectLocal(JdkHttpTransport.java:97)
at com.sforce.ws.transport.JdkHttpTransport.connectLocal(JdkHttpTransport.java:92)
at com.sforce.ws.transport.JdkHttpTransport.connect(JdkHttpTransport.java:88)
at com.sforce.ws.transport.SoapConnection.send(SoapConnection.java:94)

我无法弄清楚如何解决这个问题。正如错误消息“Falied to connect to http://login.salesforce.com./ ...”,我应该在Eclipse中启用某些设置吗?

此致 萨姆

2 个答案:

答案 0 :(得分:1)

你是代理人吗?

如果是这样,请在eclipse中启用代理设置 preferences general network

答案 1 :(得分:-1)

...

  String proxyHost = getUserInput("Enter proxy host:");
  String proxyPort = getUserInput("Enter proxy port:");

  try {

...

     if(proxyHost!=null && !proxyHost.trim().equals("")){
      config.setProxy(proxyHost, Integer.valueOf(proxyPort));
     }

...