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
arishi0arishi0 

Issue w/ web service in Java connecting to SF from behind a proxy

 

I wrote a web service to connect to a SF org and it should retrieve some account data and then it would do some validations and upload the results into another org. I cannot use S2S since the account structure and workings are way too complex and there are issues. Anyways, my issue is that when I run the code I get an error such as this(highlighted in red):

 

Sforce service created.

Error logging in to Salesforce.com HTTP transport error: java.net.UnknownHostException: login.salesforce.com

Application complete.

 

 

I believe this may have something to do with the fact that I am behind a proxy server, but my problem is that I do not know where do I need to go and edit the code to add the proxy credentials. Below is the code that executes and also the SforceService is instantiated and used to call the login method.

 

 


public Client() {
// upon instantiation invoke process to perform the application logic
doAccountUpdate();
}

// entry point method
public static void main(String[] args) {
new Client();
System.out.println("Application complete.");
}

// this holds the application logic
private void doAccountUpdate() {

// initialize the force.com web services API binding in line with JAX-WS RI
try {
URL wsdlLocation = this.getClass().getClassLoader().getResource("enterprise.wsdl");
if (wsdlLocation == null) {
WebServiceException e = new WebServiceException("enterprise.wsdl not found!");
throw e;
}
// instantiate a new instance of the binding
port = new SforceService(wsdlLocation, new QName("urn:enterprise.soap.sforce.com", "SforceService")).getSoap();


System.out.println("Sforce service created.");
} catch (WebServiceException wse) {
System.out.println("Error creating salesforce port " + wse.getMessage());
}

// initiate a login to salesforce.com
if (doSHSLogin()) {
//do some stuff with accounts
}
}

// this function encapsulates the logic necessary to login to salesforce.com
private boolean doSHSLogin() {
// declare local vars
boolean returnVal = false; // return value
LoginResult loginResponse = null; // object to store the result of the login operation
SessionHeader sh = null; // object to store information about the session
JAXBContext jc = null; // ojbect to provide access to the JAXB API

// These values where set this way for sharing purposes in discussion boards, they are not my real credentials
String userName = "myusername";
String pwd = "mypassword";
String token = "mytoken";


// attempt the login to salesforce.com
try {
loginResponse = port.login(userName, pwd + token);
returnVal = true;

// output some information about the newly created session to the screen
System.out.println("Login was successfull.");
System.out.print("The returned session id is: ");
System.out.println(loginResponse.getSessionId());
System.out.print("Your logged in user id is: ");
System.out.println(loginResponse.getUserId() + " \n\n");

} catch (Exception e) {
System.out.println("Error logging in to Salesforce.com " + e.getMessage());
return false;
}

/* Once the client application has logged in successfully, it will use
* the results of the login call to reset the endpoint of the service
* to the virtual server instance that is servicing your organization
*/
WSBindingProvider bindingProvider = ((WSBindingProvider) port);
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, loginResponse.getServerUrl());

/* The sample client application now has an instance of the SforceService
* that is pointing to the correct endpoint. Next, the sample client
* application sets a persistent SOAP header (to be included on all
* subsequent calls that are made with SforceService) that contains the
* valid sessionId for our login credentials. To do this, the sample
* client application creates a new SessionHeader object and persist it to
* the SforceService. Add the session ID returned from the login to the
* session header
*/
sh = new SessionHeader();
sh.setSessionId(loginResponse.getSessionId());
try {
jc = JAXBContext.newInstance("com.salesforce.sei");
bindingProvider.setOutboundHeaders(Headers.create((JAXBRIContext) jc, sh));
} catch (JAXBException e) {
System.out.println("Error creating JAXBContext instance " + e.getMessage());
return false;
}

//Enable GZip compression for subsequent API requests
Map<String, List<String>> httpHeaders = new HashMap<String, List<String>>();
httpHeaders.put("Content-Encoding", Collections.singletonList("gzip"));
httpHeaders.put("Accept-Encoding", Collections.singletonList("gzip"));
Map<String, Object> reqContext = bindingProvider.getRequestContext();
reqContext.put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);

// return the result of the attempt to login
return returnVal;
}

}

 This is my SforceService class info

 

 

package com.salesforce.sei;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;


/**
 * Sforce SOAP API
 * 
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "SforceService", targetNamespace = "urn:enterprise.soap.sforce.com", wsdlLocation = "file:/C:/Documents%20and%20Settings/sg0208508/workspace/integrationTest/src/enterprise.wsdl")
public class SforceService
    extends Service
{

    private final static URL SFORCESERVICE_WSDL_LOCATION;
    private final static Logger logger = Logger.getLogger(com.salesforce.sei.SforceService.class.getName());

    static {
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = com.salesforce.sei.SforceService.class.getResource(".");
            url = new URL(baseUrl, "file:/C:/Documents%20and%20Settings/sg0208508/workspace/integrationTest/src/enterprise.wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'file:/C:/Documents%20and%20Settings/sg0208508/workspace/integrationTest/src/enterprise.wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        SFORCESERVICE_WSDL_LOCATION = url;
    }

    public SforceService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public SforceService() {
        super(SFORCESERVICE_WSDL_LOCATION, new QName("urn:enterprise.soap.sforce.com", "SforceService"));
    }

    /**
     * 
     * @return
     *     returns Soap
     */
    @WebEndpoint(name = "Soap")
    public Soap getSoap() {
        return super.getPort(new QName("urn:enterprise.soap.sforce.com", "Soap"), Soap.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns Soap
     */
    @WebEndpoint(name = "Soap")
    public Soap getSoap(WebServiceFeature... features) {
        return super.getPort(new QName("urn:enterprise.soap.sforce.com", "Soap"), Soap.class, features);
    }

}


 

Can anyone help me shed some light into this issue???

 

Thank you!!

 

 

Rick.BanisterRick.Banister

 

System.setProperty("http.proxyHost", "ProxyHostGoesHere");

System.setProperty("http.proxyPort", "ProxyPortGoesHere");

 

/* Required if proxy server uses user/password authentication */

System.setProperty("http.proxyUser", "ProxyUserGoesHere");

System.setProperty("http.proxyPassword", "passwordGoesHere");

 

Enjoy

arishi0arishi0

Hello,

 

Thank you! looks great BUT one more question...where do I put this piece of code? can you tell me, this is my first web service so I am a bit lost.

Rick.BanisterRick.Banister

It goes anywhere in the code before you attempt the logon.

arishi0arishi0

Hello,

 

I am still getting this same error:

 

 

Sforce service created.
Error logging in to Salesforce.com HTTP transport error: java.net.UnknownHostException: login.salesforce.com
Application complete.

 

Sforce service created.Error logging in to Salesforce.com HTTP transport error: java.net.UnknownHostException: login.salesforce.comApplication complete.

 

 

I added the code you gave me to the same login method but still getting the error. Just replaced the username, password and proxy host to just text so not to expose this info in the post, but in the real code I have the correct values.

 

 

 

private boolean doSHSLogin() {

     // declare local vars

     boolean returnVal = false; // return value

     LoginResult loginResponse = null; // object to store the result of the login operation

     SessionHeader sh = null; // object to store information about the session

     JAXBContext jc = null; // ojbect to provide access to the JAXB API

    

     // CHANGE ME  - update the values below to login to salesforce.com

     String userName = "mauricio.ramos@sabre-holdings.com";

        String pwd = "TrompiZ#1";

        String token = "0n1mgmbPyAWIafmn0SNWIPZY";

        System.setProperty("http.proxyHost", "IP address of Proxy");

        System.setProperty("http.proxyPort", "80");

 

        /* Required if proxy server uses user/password authentication */

        String proxyUser = "myIntranetuserName";

        System.setProperty("http.proxyUser", proxyUser);

        System.setProperty("http.proxyPassword", "intranet password");

 

 

        // attempt the login to salesforce.com

        try {

         loginResponse = port.login(userName, pwd + token);

         returnVal = true;

        

            // output some information about the newly created session to the screen

            System.out.println("Login was successfull.");

            System.out.print("The returned session id is: ");

            System.out.println(loginResponse.getSessionId());

            System.out.print("Your logged in user id is: ");

            System.out.println(loginResponse.getUserId() + " \n\n");

 

        } catch (Exception e) {

         System.out.println("Error logging in to Salesforce.com " + e.getMessage());

            return false;

        }

 

SuperfellSuperfell

You should check the documentation for the soap library you're using it tends to vary by toolkit, not all the tools will pickup the default system defined properties for the proxy config.