2016-07-27 32 views
1

我正在尝试为HipChat创建一个通知,不幸的是他们的HipChat-CS(https://github.com/KyleGobel/Hipchat-CS)nuget包不起作用。所以,我想调查一下如何手动发送通知,但他们的示例使用了我不熟悉的cURL。我想借助他们的例子,并将它变成我可以用于我的C#应用​​程序的东西。任何帮助将不胜感激!以下是cURL示例:如何将HipChat curl文章转换为C#?

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

“不工作”不是错误消息。不要假设包装已损坏,请尝试找出发生了什么问题。最有可能的是破解的代码,而不是软件包。至于这个例子,这是一个简单的POST。您可以使用HttpClient执行POST请求。至于cURL的语法 - 只需查找参考或在命令行上运行即可。 –

回答

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

要使用HipChat-CS,我不得不手动卸载自动下载相关程序包ServiceStack,并手动安装特定的版本,4.0.56,ServiceStack的。一切工作,一旦我做到了