2016-07-27 31 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的。一切工作,一旦我做到了