3分鐘學會使用程式發送msn訊息,dotNet C#篇(二)
http://rd-program.blogspot.com/2008/11/3msn-net-c.html
以下內容也可參考 http://rd-program.blogspot.com/2008/11/3msn-net-c.html
全域的物件及變數
WebClient wc = new WebClient();
string SPID = "";
步驟一:取得SPID(GETSPID)
private void button1_Click(object sender, EventArgs e)
{
string url="http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
string method="POST";
string postdata = "FUNC=GETSPID&USERID=apiblogt2&PASSWD=msnsdkt";
byte[] postbyte=Encoding.UTF8.GetBytes(postdata);
byte[] resb = wc.UploadData(url, method, postbyte);
string resmsg = Encoding.UTF8.GetString(resb);
resmsg = resmsg.Replace("\n", "");
string[] arrstr = resmsg.Split('\t');
if (arrstr[0] == "1")
{
SPID = arrstr[1];
MessageBox.Show("取得SPID成功: SPID= " + SPID);
}
else
{
SPID = "";
MessageBox.Show("取得SPID失敗: " + resmsg);
}
}
步驟二:將帳號註冊為msn機器人的連絡人(REGISTER)
下述: txtmsnacc.Text 為msn/yahoo帳號
private void button2_Click(object sender, EventArgs e)
{
string url = "http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
string method = "POST";
string postdata = "FUNC=REGISTER&uids=" + txtmsnacc.Text + "&session=" + SPID;
byte[] postbyte = Encoding.UTF8.GetBytes(postdata);
byte[] resb = wc.UploadData(url, method, postbyte);
string resmsg = Encoding.UTF8.GetString(resb);
resmsg = resmsg.Replace("\n", "");
string[] arrstr = resmsg.Split('\t');
if (arrstr[0] == "1")
{
MessageBox.Show("註冊成功: " + resmsg);
}
else
{
MessageBox.Show("註冊失敗: " + resmsg);
}
}
步驟三:發送訊息(SENDMSG)
private void button3_Click(object sender, EventArgs e)
{
string msg = "msnSDK測試訊息";
string url = "http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
string method = "POST";
string postdata = "FUNC=SENDMSG&uids=" + txtmsnacc.Text + "&msg=" + msg + "&encoding=utf-8&flags=0&session=" + SPID;
byte[] postbyte = Encoding.UTF8.GetBytes(postdata);
byte[] resb = wc.UploadData(url, method, postbyte);
string resmsg = Encoding.UTF8.GetString(resb);
resmsg = resmsg.Replace("\n", "");
string[] arrstr = resmsg.Split('\t');
if (arrstr[0] == "1")
{
MessageBox.Show("發送訊息成功: " + resmsg);
}
else
{
MessageBox.Show("發送訊息失敗: " + resmsg);
}
}
其他更詳細的參數可以參考msnSDK訊息控制開發套件程式介面說明書