惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - tiasys

guid与Base64编码互相转换 xml序列化与反序列化工具 Win10 15063 开始运行不保存历史记录原因和解决方法 win10 localhost 解析为 ipv6地址 ::1 的解决办法 在VisualStudio中应该使用什么字体 S7-200系列PLC与WINCC以太网通信CP243i的实例 超棒的 15 款 Bootstrap UI 编辑器 NuGet学习笔记(转) SQLServer公历转农历函数(1900年-2049年) WPF 的datagrid 列名中没有显示下划线是怎么回事? Vmware9.0打开早期版本报错:this virtual machine’s policies are too old to be run by this version of vmware workstation” Remove WebCakeDesktop Windows8[启用WCF IIS8 svc文件访问服务] Windows8[启用IIS8 asp.net功能] Windows8 [未启用.net3.5功能] Windows8[Web应用程序项目***已配置为使用IIS。无法访问IIS元数据库,您没有足够的特权访问计算机上的IIS网站] Oracle 正确删除archivelog文件 SQLSERVER 删除重复记录 黑链代码
.NET实现Wap飞信协议
tiasys · 2012-09-05 · via 博客园 - tiasys

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 &amp;&amp; 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 &amp;&amp; result.Contains("短信发送成功!");
        }
 
        /// <summary>
        
/// 登陆
        
/// </summary>
        
/// <returns></returns>
        public string Login()
        {
            string uri = server + "/im/login/inputpasssubmit1.action";
            return PostWithCookie(uri, string.Format("m={0}&amp;pass={1}&amp;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);
            }
        }
    }

 #region http请求
        public string HttpPost(string strUrl, string strData)
        {
            byte[] argsBytes = System.Text.Encoding.Default.GetBytes(strData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "Post";
            request.Timeout = 8000;
            request.CookieContainer = m_CookieContainer;


            using (Stream outStream = request.GetRequestStream())
            {
                outStream.Write(argsBytes, 0, argsBytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            String strRet = "";
            Encoding coding = Encoding.Default;
            if (response.CharacterSet != null && response.CharacterSet.Trim() != "")
            {
                try
                {
                    coding = Encoding.GetEncoding(response.CharacterSet);
                }
                catch { }
            }


            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(dataStream, coding))
                {
                    strRet = reader.ReadToEnd();
                }
            }
            return strRet;
        }
        #endregion

}