【问题标题】:HttpRequest POST to RESTful web service - Salesforce Apex CalloutHttpRequest POST 到 RESTful Web 服务 - Salesforce Apex 标注
【发布时间】:2014-08-18 05:59:07
【问题描述】:

我创建了一个实现以下接口的 RESTful Web 服务(C#、WCF):

public interface ITestService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
     ResponseFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Bare,
     UriTemplate = "?s={aStr}")]
    string Test(string aStr);
}

Test() 方法只返回给定的任何内容(或默认的"test" 字符串) - 还有调用该方法时的时间戳

该服务是公开的,所以当我在任何浏览器中输入网址时:

http://xx.xxx.xxx.xx:41000/TestService/web/

它返回 json "test" 字符串(或任何可能在末尾输入的 ?s=...)。


我希望Salesforce 将数据发布到此网络服务。

我的 apex 类看起来像这样 - 当一个对象插入 Salesforce 时它会被触发:

public class WebServiceCallout 
{
    @future (callout=true)
    public static void sendNotification(String name) 
    {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('http://xx.xxx.xxx.xx:41000/TestService/web/');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody('');

        try 
        {
            res = http.send(req);
        } 
        catch(System.CalloutException e) 
        {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }
    }
}

将对象插入 Salesforce 时,Apex 作业 部分会显示 sendNotification() 方法已完成。但是该服务从未通过该方法获取 POST。 (注:已添加远程站点设置中的服务IP)。

我的语法有问题吗?

(在这个阶段,我只想让 Salesforce 调用 Web 服务 - 甚至不向其发布任何内容)


作为示例,我创建了一个示例Console Application,可以正常发布到服务。

internal static void Main(string[] args)
{
    Uri address = new Uri("http://xx.xxx.xxx.xx:41000/TestService/web/");

    // Create the web request
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

    // Set type to POST
    request.Method = "POST";
    request.ContentType = "application/json";

    // Create the data we want to send  
    var postData = "";

    // Create a byte array of the data we want to send
    var byteData = UTF8Encoding.UTF8.GetBytes(postData);

    // Set the content length in the request headers
    request.ContentLength = byteData.Length;

    // Write data
    using (var stream = request.GetRequestStream())
    {
        stream.Write(byteData, 0, byteData.Length);
    }

    // Get response  
    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(
        response.GetResponseStream()
        ).ReadToEnd();

    Console.Writeline(responseString);
}

为什么我在 Salesforce 中的 Apex 类标注不正确?

【问题讨论】:

    标签: c# .net wcf rest salesforce


    【解决方案1】:

    我发现了问题所在。我没有在远程站点设置中指定端口(什么!!)

    上面的代码示例应该可以完美运行(至少它们对我有用)


    提示未来遇到此问题的人

    设置 -> 安全 -> 远程站点设置

    服务(远程站点 URL)应采用这种格式 - 无论您在何处托管它:

    http://xx.xxx.xxx.xx:41000
    

    要测试上面的代码,请转到:

    [你的名字] -> 开发者控制台 -> 调试 -> 打开执行匿名窗口

    并复制以下行:

    WebServiceCallout.sendNotification('Test');
    

    然后点击执行

    代码执行,您应该会看到一个执行日志(以及其中的任何错误)。

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2016-02-18
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多