.NET实现Wap飞信协议

简介:

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  

}



本文转自tiasys博客园博客,原文链接:http://www.cnblogs.com/tiasys/archive/2012/09/05/2672209.html,如需转载请自行联系原作者

相关文章
|
10月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
|
存储 数据采集 网络协议
【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
随着工业化的发展,目前越来越多的开发,从互联网走向传统行业。其中,工业领域也是其中之一,包括各大厂也都在陆陆续续加入工业4.0的进程当中。
305 0
【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
|
消息中间件 资源调度 Kafka
Flink / Kafka - Recovery is suppressed by FixedDelayRestartBackoffTimeStrategy 排查与修复 ———————————————— 版权声明:本文为CSDN博主「BIT_666」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/BIT_666/article/details/125419738
使用 Flink - Kafka 接数据 Source 时程序报错:org.apache.flink.runtime.JobException: Recovery is suppressed by FixedDelayRestartBackoffTimeStrategy,任务每次启动后持续10min左右,然后 RUNNING -> FAILED,如此重启失败了多次。
2682 0
Flink / Kafka - Recovery is suppressed by FixedDelayRestartBackoffTimeStrategy 排查与修复  ———————————————— 版权声明:本文为CSDN博主「BIT_666」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/BIT_666/article/details/125419738
|
网络虚拟化
pip安装torchvision报错ProxyError: Conda cannot proceed due to an error in your proxy configuration. ———————————————— 版权声明:本文为CSDN博主「山顶夕景」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_35812205/article/details/119078793
报错:ProxyError: Conda cannot proceed due to an error in your proxy configuration. 提示:Check for typos and other configuration errors in any ‘.netrc’ file in your home directory, any environment variables ending in ‘_PROXY’, and any other system-wide proxy
864 0
|
关系型数据库 MySQL Java
|
前端开发 JavaScript
uniapp真机调试文件查找失败:‘./pages/index/index.nvue?mpType=page‘; Error: Cannot find module ‘pages/ ———————————————— 版权声明:本文为CSDN博主「前端老实人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_52691965/article/details/119241451
uniapp真机调试文件查找失败:‘./pages/index/index.nvue?mpType=page‘; Error: Cannot find module ‘pages/ ———————————————— 版权声明:本文为CSDN博主「前端老实人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_52691965/article/details/119241451