簡體   English   中英

怎么把HipChat curl post轉換成C#?

[英]How to convert this HipChat curl post to C#?

我正在嘗試為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

您可以使用HttpWebRequest建立連接並發送json有效負載。 您需要將System.Net添加到using指令。

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.

要使用HipChat-CS,我必須手動卸載自動下載的從屬軟件包ServiceStack,並手動安裝ServiceStack的特定版本4.0.56 一旦執行此操作,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM