相關(guān)資訊
本類常用軟件
-
福建農(nóng)村信用社手機(jī)銀行客戶端下載下載量:584204
-
Windows優(yōu)化大師下載量:416898
-
90美女秀(視頻聊天軟件)下載量:366961
-
廣西農(nóng)村信用社手機(jī)銀行客戶端下載下載量:365699
-
快播手機(jī)版下載量:325855
09年的時(shí)候,我用C#實(shí)現(xiàn)了簡(jiǎn)單的飛信協(xié)議,并開了源,詳情請(qǐng)查看這里。直到現(xiàn)在還有童鞋發(fā)郵件給我向咨詢或是所要代碼。但是由于飛信協(xié)議有個(gè)幾次的升級(jí),我那個(gè)庫基本上沒什么用了。由于工作比較忙,也一直沒有去管他。前兩天,我用這個(gè)項(xiàng)目申請(qǐng)了sinaapp的中級(jí)開發(fā)者人證,居然僥幸通過了,所以覺得有必要更新一下代碼了。
網(wǎng)上查了一下有關(guān)飛信協(xié)議的最新的情況,沒有什么進(jìn)展,我也不想自己通過抓包去分析飛信協(xié)議了,畢竟那樣會(huì)比較耗時(shí)。正當(dāng)我準(zhǔn)備放棄的時(shí)候發(fā)現(xiàn)有人用php實(shí)現(xiàn)了wap飛信的協(xié)議。不看不知道,一看嚇一跳,協(xié)議非常簡(jiǎn)單,總共代碼也不超過100行。
我花了一小段時(shí)間,將那php的代碼翻譯成了C#的,測(cè)試了一下,還挺好用的,速度也挺快的。
下面是具體的代碼實(shí)現(xiàn),看看非常簡(jiǎn)單,占內(nèi)存和CPU肯定也會(huì)非常的少。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Web;
using System.Text.RegularExpressions;
namespace Fetion
{
public class WapFetion
{
private static string server = "http://f.10086.cn";
private string mobile;
private string password;
private CookieContainer cookies = new CookieContainer();
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="mobile">手機(jī)號(hào)碼</param>
/// <param name="password">密碼</param>
public WapFetion(string mobile, string password)
{
this.mobile = mobile;
this.password = password.ToUrlEncode();
}
protected string PostWithCookie(string uri, string data)
{
using (HttpWebClient hwc = new HttpWebClient(cookies))
{
hwc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
return Encoding.UTF8.GetString(hwc.UploadData(uri, Encoding.UTF8.GetBytes(data)));
}
}
protected string GetUid(string mobile)
{
string uri = server + "/im/index/searchOtherInfoList.action";
string data = "searchText=" + mobile;
string result = PostWithCookie(uri, data);
Match mc = Regex.Match(result, @"toinputMsg\.action\?touserid=(\d+)");
if (mc.Success)
{
return mc.Result("$1");
}
return null;
}
protected bool ToUid(string uid, string message)
{
string uri = server + "/im/chat/sendMsg.action?touserid=" + uid;
string data = "msg=" + message.ToUrlEncode();
string result = PostWithCookie(uri, data);
return result != null && result.Contains("發(fā)送消息成功!");
}
protected bool ToMyself(string message)
{
string uri = server + "/im/user/sendMsgToMyselfs.action";
string data = "msg=" + message.ToUrlEncode();
string result = PostWithCookie(uri, data);
return result != null && result.Contains("短信發(fā)送成功!");
}
/// <summary>
/// 登陸
/// </summary>
/// <returns></returns>
public string Login()
{
string uri = server + "/im/login/inputpasssubmit1.action";
return PostWithCookie(uri, string.Format("m={0}&pass={1}&loginstatus=1", mobile, password));
}
/// <summary>
/// 注銷
/// </summary>
/// <returns></returns>
public string Logout()
{
string uri = server + "/im/index/logoutsubmit.action";
return PostWithCookie(uri, "");
}
/// <summary>
/// 通過手機(jī)號(hào),給自己會(huì)好友發(fā)送消息
/// </summary>
/// <param name="mobile">手機(jī)號(hào)</param>
/// <param name="message">消息</param>
/// <returns></returns>
public bool Send(string mobile, string message)
{
if (string.IsNullOrWhiteSpace(message))
{
return false;
}
if (mobile == this.mobile)
{
return ToMyself(message);
}
else
{
string uid = GetUid(mobile);
if (uid == null)
{
return false;
}
return ToUid(uid, message);
}
}
}
}
從上面可以看出,這個(gè)代碼是相當(dāng)?shù)暮?jiǎn)單,用到了一個(gè)HttpWebClient,其實(shí)就是對(duì)WebClient的簡(jiǎn)單的封裝,實(shí)現(xiàn)了Cookie的傳遞。
整個(gè)工程的代碼:WapFetion.NET