.NET實(shí)現(xiàn)Wap飛信協(xié)議
作者: 來源: 發(fā)布時(shí)間:2012-1-8 16:19:03 點(diǎn)擊:
09年的時(shí)候,我用C#實(shí)現(xiàn)了簡單的飛信協(xié)議,并開了源,詳情請查看這里。直到現(xiàn)在還有童鞋發(fā)郵件給我向咨詢或是所要代碼。但是由于飛信協(xié)議有個(gè)幾次的升級,我那個(gè)庫基本上沒什么用了。由于工作比較忙,也一直沒有去管他。前兩天,我用這個(gè)項(xiàng)目申請了sinaapp的中級開發(fā)者人證,居然僥幸通過了,所以覺得有必要更新一下代碼了。
網(wǎng)上查了一下有關(guān)飛信協(xié)議的最新的情況,沒有什么進(jìn)展,我也不想自己通過抓包去分析飛信協(xié)議了,畢竟那樣會比較耗時(shí)。正當(dāng)我準(zhǔn)備放棄的時(shí)候發(fā)現(xiàn)有人用php實(shí)現(xiàn)了wap飛信的協(xié)議。不看不知道,一看嚇一跳,協(xié)議非常簡單,總共代碼也不超過100行。
我花了一小段時(shí)間,將那php的代碼翻譯成了C#的,測試了一下,還挺好用的,速度也挺快的。
下面是具體的代碼實(shí)現(xiàn),看看非常簡單,占內(nèi)存和CPU肯定也會非常的少。
View Code
using System;
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ī)號碼</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ī)號,給自己會好友發(fā)送消息
/// </summary>
/// <param name="mobile">手機(jī)號</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);
}
}
}
}
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ī)號碼</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ī)號,給自己會好友發(fā)送消息
/// </summary>
/// <param name="mobile">手機(jī)號</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);
}
}
}
}
[收藏此文章]
本類熱門
本類推薦
本站熱門
東坡下載 & 分享互聯(lián)網(wǎng) Copyright(C) 2008-2010 tipsywinegypsy.com All Rights Reserved! 網(wǎng)站備案/許可證號:京ICP備10000128號-1