Skip to main content
Notice removed Current answers are outdated by CommunityBot
Bounty Ended with Daniel Ballinger's answer chosen by CommunityBot
Tweeted twitter.com/#!/StackSalesforce/status/527149398866657282
Updated question now that they told me it was whitelisted.
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72
HttpRequest req = new HttpRequest();
req.setEndpoint(SOME_URL + '/etc/authentication/Login?_contenttype=text/xml');//?' + params);
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//I tried all 3 versions below.  
//String params = 'username=' + EncodingUtil.urlEncode('userid1'userName, 'UTF-8') + '&password=' + EncodingUtil.urlEncode('P@ssword1'password, 'UTF-8');
String params = 'username=userid1&password=P@ssword1';'username=' + userName + '&password=' + password;

//This version gives a different error that says 'Both username and password are required.'
//String params = EncodingUtil.urlEncode('username=userid1&password=P@ssword1''username=' + userName + '&password=' + password, 'UTF-8');
req.setBody(params);
Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('body = ' + res.getBody());
HttpRequest req = new HttpRequest();
req.setEndpoint(SOME_URL + '/etc/authentication/Login?_contenttype=text/xml');//?' + params);
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//I tried all 3 versions below.  
//String params = 'username=' + EncodingUtil.urlEncode('userid1', 'UTF-8') + '&password=' + EncodingUtil.urlEncode('P@ssword1', 'UTF-8');
String params = 'username=userid1&password=P@ssword1';

//This version gives a different error that says 'Both username and password are required.'
//String params = EncodingUtil.urlEncode('username=userid1&password=P@ssword1', 'UTF-8');
req.setBody(params);
Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('body = ' + res.getBody());
HttpRequest req = new HttpRequest();
req.setEndpoint(SOME_URL + '/etc/authentication/Login?_contenttype=text/xml');
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//I tried all 3 versions below.  
//String params = 'username=' + EncodingUtil.urlEncode(userName, 'UTF-8') + '&password=' + EncodingUtil.urlEncode(password, 'UTF-8');
String params = 'username=' + userName + '&password=' + password;

//This version gives a different error that says 'Both username and password are required.'
//String params = EncodingUtil.urlEncode('username=' + userName + '&password=' + password, 'UTF-8');
req.setBody(params);
Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('body = ' + res.getBody());
Notice added Current answers are outdated by dphil
Bounty Started worth 50 reputation by dphil
Updated question now that they told me it was whitelisted.
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72

I'm having issues correctly getting Salesforce to connect to one of our external systems. I They recently whitelisted the range of Salesforce addresses (At least so they said). Our Sandbox is coming from cs7.

96.43.144.0/20 _ subnet mask: 255.255.240.0
136.146.0.0/15 _ subnet mask: 255.254.0.0
182.50.76.0/22 _ subnet mask: 255.255.252.0
204.14.232.0/21 _ subnet mask: 255.255.248.0

I have 2 different Chrome plugin REST clients that are managing the login call fine.

Advanced REST Client:

Advanced REST Client

Postman REST Client:

Postman REST Client

I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

URL url = new URL("https://testweb1.somesystem.com/system2SOME_URL + '/etc/authentication/Login?_contenttype=text/xml");
String username = "userid";
String password = "userpw";
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have so farcreated in Apex to replicate these calls:

HttpRequest req = new HttpRequest();
 
req.setEndpoint('https://testweb1.somesystem.com/system2SOME_URL + '/etc/authentication/Login?_contenttype=text/xml');
String params//?' =+ 'username=userid&password=userpw';params);
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 
//Content-LengthI istried requiredall in3 thisversions callbelow. as well
req.setHeader('Content-Length', //String.valueOf( params.length()));
req = 'username=' + EncodingUtil.setHeaderurlEncode('username''userid1', 'userid''UTF-8');
req + '&password=' + EncodingUtil.setHeaderurlEncode('password''P@ssword1', 'userpw''UTF-8');
String params = 'username=userid1&password=P@ssword1';

DOM//This version gives a different error that says 'Both username and password are required.Document'
//String docparams = new DOMEncodingUtil.DocumenturlEncode('username=userid1&password=P@ssword1', 'UTF-8');
req.setBody(params);
Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('response'body = ' + res.getBody());

I thought about trying to pass some form of DOM through as the body but it doesn't appear like that is what they are doing withMy code always returns the Javamessage: 'Invalid stored procedure name. On top of' I am thinking that potentially the documentation doesn't seem to be helping me very well to build an example. Does anyoneSalesforce IP isn't correctly whitelisted but I really have any recommendations forno proof since they told me?

Using the postman REST client I can create a successful connection as of this morning it was now whitelisted.

enter image description here Any ideas?

I'm having issues correctly getting Salesforce to connect to one of our external systems. I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

URL url = new URL("https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml");
String username = "userid";
String password = "userpw";
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have so far in Apex:

HttpRequest req = new HttpRequest();
 
req.setEndpoint('https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml');
String params = 'username=userid&password=userpw';
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 
//Content-Length is required in this call as well
req.setHeader('Content-Length', String.valueOf(params.length()));
req.setHeader('username', 'userid');
req.setHeader('password', 'userpw');

DOM.Document doc = new DOM.Document();

Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('response = ' + res.getBody());

I thought about trying to pass some form of DOM through as the body but it doesn't appear like that is what they are doing with the Java. On top of that the documentation doesn't seem to be helping me very well to build an example. Does anyone have any recommendations for me?

Using the postman REST client I can create a successful connection.

enter image description here

I'm having issues correctly getting Salesforce to connect to one of our external systems. They recently whitelisted the range of Salesforce addresses (At least so they said). Our Sandbox is coming from cs7.

96.43.144.0/20 _ subnet mask: 255.255.240.0
136.146.0.0/15 _ subnet mask: 255.254.0.0
182.50.76.0/22 _ subnet mask: 255.255.252.0
204.14.232.0/21 _ subnet mask: 255.255.248.0

I have 2 different Chrome plugin REST clients that are managing the login call fine.

Advanced REST Client:

Advanced REST Client

Postman REST Client:

Postman REST Client

I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

URL url = new URL(SOME_URL + '/etc/authentication/Login?_contenttype=text/xml");
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have created in Apex to replicate these calls:

HttpRequest req = new HttpRequest();
req.setEndpoint(SOME_URL + '/etc/authentication/Login?_contenttype=text/xml');//?' + params);
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//I tried all 3 versions below.  
//String params = 'username=' + EncodingUtil.urlEncode('userid1', 'UTF-8') + '&password=' + EncodingUtil.urlEncode('P@ssword1', 'UTF-8');
String params = 'username=userid1&password=P@ssword1';

//This version gives a different error that says 'Both username and password are required.'
//String params = EncodingUtil.urlEncode('username=userid1&password=P@ssword1', 'UTF-8');
req.setBody(params);
Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('body = ' + res.getBody());

My code always returns the message: 'Invalid stored procedure name.' I am thinking that potentially the Salesforce IP isn't correctly whitelisted but I really have no proof since they told me as of this morning it was now whitelisted. Any ideas?

Post Undeleted by dphil
Post Deleted by dphil
added 156 characters in body
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72

I'm having issues correctly getting Salesforce to connect to one of our external systems. I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

Java:

URL url = new URL("https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml");
String username = "userid";
String password = "userpw";
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have so far in Apex:

HttpRequest req = new HttpRequest();

req.setEndpoint('https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml');
String params = 'username=userid&password=userpw';
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');

//Content-Length is required in this call as well
req.setHeader('Content-Length', String.valueOf(params.length()));
req.setHeader('username', 'userid');
req.setHeader('password', 'userpw');

DOM.Document doc = new DOM.Document();

Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('response = ' + res.getBody());

I thought about trying to pass some form of DOM through as the body but it doesn't appear like that is what they are doing with the Java. On top of that the documentation doesn't seem to be helping me very well to build an example. Does anyone have any recommendations for me?

Using the postman REST client I can create a successful connection.

enter image description here

I'm having issues correctly getting Salesforce to connect to one of our external systems. I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

Java:

URL url = new URL("https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml");
String username = "userid";
String password = "userpw";
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have so far in Apex:

HttpRequest req = new HttpRequest();

req.setEndpoint('https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml');
String params = 'username=userid&password=userpw';
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');

//Content-Length is required in this call as well
req.setHeader('Content-Length', String.valueOf(params.length()));
req.setHeader('username', 'userid');
req.setHeader('password', 'userpw');

DOM.Document doc = new DOM.Document();

Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('response = ' + res.getBody());

I thought about trying to pass some form of DOM through as the body but it doesn't appear like that is what they are doing with the Java. On top of that the documentation doesn't seem to be helping me very well to build an example. Does anyone have any recommendations for me?

I'm having issues correctly getting Salesforce to connect to one of our external systems. I was given some Java code that is being used to connect but I'm having a hard time converting this to Salesforce.

Java:

URL url = new URL("https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml");
String username = "userid";
String password = "userpw";
String urlParams = "username=" + username + "&password=" + password;

HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

uc.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
                                
uc.setUseCaches (false);
uc.setDoInput(true);
uc.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (uc.getOutputStream());
wr.writeBytes (urlParams);
wr.flush ();
wr.close ();

if(uc.getContent()!=null) {
   InputStream in = (InputStream) uc.getContent();
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(in);
                
   NodeList recordSet = doc.getDocumentElement().getElementsByTagName("recordset");
   String sessionId = recordSet.item(0).getChildNodes().item(1).getAttributes().getNamedItem("UserSessionID").getNodeValue(); 
}

The code I have so far in Apex:

HttpRequest req = new HttpRequest();

req.setEndpoint('https://testweb1.somesystem.com/system2/etc/authentication/Login?_contenttype=text/xml');
String params = 'username=userid&password=userpw';
req.setMethod('POST');

req.setHeader('Content-Type', 'application/x-www-form-urlencoded');

//Content-Length is required in this call as well
req.setHeader('Content-Length', String.valueOf(params.length()));
req.setHeader('username', 'userid');
req.setHeader('password', 'userpw');

DOM.Document doc = new DOM.Document();

Http hp = new Http();

HTTPResponse res = hp.send(req);
System.debug('response = ' + res.getBody());

I thought about trying to pass some form of DOM through as the body but it doesn't appear like that is what they are doing with the Java. On top of that the documentation doesn't seem to be helping me very well to build an example. Does anyone have any recommendations for me?

Using the postman REST client I can create a successful connection.

enter image description here

added 55 characters in body
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72
Loading
deleted 8 characters in body
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72
Loading
Source Link
dphil
  • 6.2k
  • 2
  • 37
  • 72
Loading