相关资讯
本类常用软件
-
福建农村信用社手机银行客户端下载下载量:584204
-
Windows优化大师下载量:416898
-
90美女秀(视频聊天软件)下载量:366961
-
广西农村信用社手机银行客户端下载下载量:365699
-
快播手机版下载量:325855
09年的时候,我用C#实现了简单的飞信协议,并开了源,详情请查看这里。直到现在还有童鞋发邮件给我向咨询或是所要代码。但是由于飞信协议有个几次的升级,我那个库基本上没什么用了。由于工作比较忙,也一直没有去管他。前两天,我用这个项目申请了sinaapp的中级开发者人证,居然侥幸通过了,所以觉得有必要更新一下代码了。
网上查了一下有关飞信协议的最新的情况,没有什么进展,我也不想自己通过抓包去分析飞信协议了,毕竟那样会比较耗时。正当我准备放弃的时候发现有人用php实现了wap飞信的协议。不看不知道,一看吓一跳,协议非常简单,总共代码也不超过100行。
我花了一小段时间,将那php的代码翻译成了C#的,测试了一下,还挺好用的,速度也挺快的。
下面是具体的代码实现,看看非常简单,占内存和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>
/// 构造函数
/// </summary>
/// <param name="mobile">手机号码</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("发送消息成功!");
}
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("短信发送成功!");
}
/// <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>
/// 通过手机号,给自己会好友发送消息
/// </summary>
/// <param name="mobile">手机号</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>
/// 构造函数
/// </summary>
/// <param name="mobile">手机号码</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("发送消息成功!");
}
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("短信发送成功!");
}
/// <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>
/// 通过手机号,给自己会好友发送消息
/// </summary>
/// <param name="mobile">手机号</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);
}
}
}
}
从上面可以看出,这个代码是相当的简单,用到了一个HttpWebClient,其实就是对WebClient的简单的封装,实现了Cookie的传递。
整个工程的代码:WapFetion.NET
热门评论
最新评论