简体   繁体   中英

How to construct a correct HttpRequest in Salesforce

I am developing an application in Salesforce that should communicate with a server to get some data. the server address is like this http://192.168.115.22/ .

this is the HttpRequest object i am constructing in my Apex code is :

HttpRequest request = new HttpRequest();
String username = "myLogin", passwd = "myPassword";
String dataUrl = 'sli=on&fli=on&login=' + username + '&password=' + passwd + 'the rest of data';

request.setMethod('POST');
request.setEndpoint('http://192.168.115.22/Services/getLicenses');
request.setHeader('Content-Type','application/x-www-form-urlencoded');
request.setHeader('Content-Length', String.valueOf(dataUrl.length()));
request.setBody(dataUrl);

Http http = new Http();
HttpResponse response = http.send(request);

But the response i am getting contains this error :

The following error was encountered while trying to retrieve the URL: http://192.168.115.22/Licgen/Service/getLicensePackage?

    Access Denied.

i don't know what is the problem with the url i am constructing, and when i write the url manually in my navigator i have access to the server.

Any help please.

This issue, in light of having already been added to the whitelist for endpoints, I think has to do with the fact that 192.168 IP addresses denote local IP addresses within a network normally.

It's likely that you can reach it because you're on your network, but Salesforce cannot see your network from the inside. You would need to use something like ngrok.com which allows you to expose a local machine to an outside available IP address.

You can also find out your router's IP address if you are at home, and port forward to your 192.168.115.22 local machine through it to expose it to the outside world, but I've had a much easier time with ngrok.com for easy integration testing with my applications.

Some Information On 192.168 addresses

(Adding this answer because my other answer could legitimately solve someone searching for the same issue above)

This issue may have to do with the fact that Salesforce prevents outside calls to un-authorized endpoints. Fortunately, it is a simple matter of adding an endpoint to the whitelist of authorized outbound connection points.

Under the setup menu option, and then again under teh security menu, there is a menu item called remote site settings . Within this option you'll see a list of sites you are allowed to access from within Salesforce. If your endpoint isn't in the list, add it and you may get a different result.

Please let me know if you have already added this to the whitelist and it is still a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM