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
msnSDK讯息控制开发套件 同时支援MSN/Yahoo即时通 讯息双向传递
ps.也请参考 msnSDK的使用流程
全域的物件及变数
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讯息控制开发套件程式介面说明书