【发布时间】: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