Skip to content

Instantly share code, notes, and snippets.

@wnoguchi
Created January 29, 2014 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnoguchi/8689276 to your computer and use it in GitHub Desktop.
Save wnoguchi/8689276 to your computer and use it in GitHub Desktop.
ChatWork APIを叩くサンプル。
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace ChatWorkMessenger
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void sendButton_Click(object sender, EventArgs e)
{
// 各種設定値
const string apiKey = "API KEY HERE";
// グループチャットを指定
const string roomId = "99999999";
//文字コードを指定する
var enc = Encoding.GetEncoding("UTF-8");
// パラメタのエンコード・構築
var postData = "body=" + Uri.EscapeDataString(messageTextBox.Text);
var postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);
// WebRequest作成
var requestUrl = string.Format("https://api.chatwork.com/v1/rooms/{0}/messages", roomId);
var req = WebRequest.Create(requestUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
// POSTデータ長を指定
req.ContentLength = postDataBytes.Length;
req.Headers.Add(string.Format("X-ChatWorkToken: {0}", apiKey));
// データをPOST送信するためのStreamを取得
var reqStream = req.GetRequestStream();
// 送信するデータを書き込む
reqStream.Write(postDataBytes, 0, postDataBytes.Length);
reqStream.Close();
// サーバーからの応答を受信する
var res = req.GetResponse();
// 応答データを受信するためのStreamを取得
var resStream = res.GetResponseStream();
// 受信して表示
var sr = new StreamReader(resStream, enc);
// 結果受信
var responseMessage = sr.ReadToEnd();
sr.Close();
MessageBox.Show(responseMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment