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:
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.
Java:
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');
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());
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?