简体   繁体   English

如何在Salesforce中构造正确的HttpRequest

[英]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. 我正在Salesforce中开发一个应与服务器通信以获取一些数据的应用程序。 the server address is like this http://192.168.115.22/ . 服务器地址是这样的http://192.168.115.22/

this is the HttpRequest object i am constructing in my Apex code is : 这是我在Apex代码中构造的HttpRequest对象是:

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. 我不知道正在构建的url有什么问题,当我在导航器中手动编写url时,我可以访问服务器。

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. 考虑到此问题已经被添加到端点的白名单中,我认为这与以下事实有关:192.168个IP地址通常表示网络中的本地IP地址。

It's likely that you can reach it because you're on your network, but Salesforce cannot see your network from the inside. 因为您在网络上,所以很可能可以到达它,但是Salesforce无法从内部看到您的网络。 You would need to use something like ngrok.com which allows you to expose a local machine to an outside available IP address. 您将需要使用类似ngrok.com的工具,使您可以将本地计算机暴露于外部可用IP地址。

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. 您也可以找到路由器的IP地址(如果您在家),并通过它转发到192.168.115.22本地计算机,以将其暴露给外界,但是ngrok.com让我度过了轻松得多的时光与我的应用程序进行集成测试。

Some Information On 192.168 addresses 有关192.168地址的一些信息

(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. 此问题可能与Salesforce阻止外部呼叫未经授权的端点有关。 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 . setup菜单选项下,然后在security菜单下,再次有一个名为“ remote site settings的菜单项。 Within this option you'll see a list of sites you are allowed to access from within Salesforce. 在此选项中,您将看到允许您从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. 如果您已将其添加到白名单中,仍然存在问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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