Campaign Monitor API通过.NET

时间:2012-10-18 21:47:23

标签: c# webhooks campaign-monitor

是否有人使用Campaign Monitor通过Campaign Monitor webhooks接收订阅更新?我已经将webhook配置为将json数据发布到我们网站上的页面,但是当我的代码收到数据时,它只是一堆零。我的代码如下:

System.IO.Stream stream = Request.InputStream;
        byte[] byteData = new byte[Convert.ToInt32(stream.Length)];
        int read = stream.Read(byteData, 0, Convert.ToInt32(stream.Length));
        string streamData = string.Empty;
        string somedata = System.Text.Encoding.ASCII.GetString(byteData).ToString();
        for (int i = 0; i < Convert.ToInt32(stream.Length); i++)
        {
            streamData = streamData + byteData[i].ToString();
        }

我已经尝试将数据发布到我的页面并且它有效 - 即上面的代码(somedata)返回我发布到页面的相同文本值。非常令人沮丧....

任何想法?我今晚失去了很多头发!!!

提前致谢 人

1 个答案:

答案 0 :(得分:2)

您可以按如下方式阅读请求正文(来自webforms应用程序):

var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();

您期望订阅活动的数据的docs specify格式为:

{
    "Events": [
        {
            "CustomFields": [
                {
                    "Key": "website", 
                    "Value": "http:\/\/example.org"
                }
            ],
            "Date": "2010-12-14 11:32:00",
            "EmailAddress": "test@example.org",
            "Name": "Test Subscriber",
            "SignupIPAddress": "53.78.123.243",
            "Type": "Subscribe"
        }
    ],
    "ListID": "96c0bbdaa54760c8d9e62a2b7ffa2e13"
}

您可以使用上面的示例数据通过向您的网址提交POST请求来自行测试。以下是使用curl的示例:

curl http://localhost:49681/receiver.aspx -v -X POST -d \
'{
    "Events": [
        {
            "CustomFields": [
                {
                    "Key": "website", 
                    "Value": "http:\/\/example.org"
                }
            ],
            "Date": "2010-12-14 11:32:00",
            "EmailAddress": "test@example.org",
            "Name": "Test Subscriber",
            "SignupIPAddress": "53.78.123.243",
            "Type": "Subscribe"
        }
    ],
    "ListID": "96c0bbdaa54760c8d9e62a2b7ffa2e13"
}'