如何将此HipChat curl帖子转换为C#?

时间:2016-07-27 16:25:35

标签: c# curl hipchat

我正在尝试为HipChat创建通知,不幸的是他们的HipChat-CS(https://github.com/KyleGobel/Hipchat-CS)nuget包不起作用。所以,我想研究如何手动发送通知,但他们的例子是使用cURL,我不熟悉。我想把他们的例子变成我可以在我的c#应用程序中使用的东西。任何帮助将不胜感激!这是cURL示例:

curl -d '{"color":"green","message":"My first notification (yey)","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://organization.hipchat.com/v2/room/2388080/notification?auth_token=authKeyHere

再次感谢你!

AJ

2 个答案:

答案 0 :(得分:3)

您可以使用HttpWebRequest建立连接并发送您的json有效负载。您需要在使用指令中添加System.Net

string jsonContent = "{\"color\":\"green\",\"message\":\"My first notification (yey)\",\"notify\":false,\"message_format\":\"text\"}";
byte[] jsonBytes = Encoding.ASCII.GetBytes(jsonContent);
var request = (HttpWebRequest) WebRequest.Create("https://organization.hipchat.com/v2/room/2388080/notification?auth_token=authKeyHere");
request.ContentType = "application/json";
request.Method = "POST";
request.ContentLength = jsonBytes.Length;

using (var reqStream = request.GetRequestStream())
{
    reqStream.Write(jsonBytes, 0, jsonBytes.Length);
    reqStream.Close();
}

 WebResponse response = request.GetResponse();
//access fields of response in order to get the response data you're looking for.

答案 1 :(得分:1)

要使用HipChat-CS,我必须手动卸载自动下载的依赖包 ServiceStack,并手动安装ServiceStack的特定版本4.0.56,。一旦我这样做,一切都有效。