发送飞信免费短信API

通过分析网页版飞信(地址:https://webim.feixin.10086.cn/),封装成自己的通用API,通过该API可以给自己和飞信好友的手机发送免费短信。


这是官网登录界面截图:


我们可以通过模拟网页浏览器实现发送飞信短信,下面是具体实现方法。


一、预览截图


1)发送测试界面


2)手机接收截图



二、关键代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Lyh.Libs.MyConverter;
using Lyh.Libs.WebUtility;

namespace Lyh.API.Feixin
{
    public class WebMessenger
    {
        private HttpClient httpClient=new HttpClient();

        /// <summary>
        /// 获取验证码图片
        /// </summary>
        /// <returns></returns>
        public Image GetPic()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetPicCode.aspx?Type=ccpsession&{0}", new Random().NextDouble());
            var parameter = new HttpParameter()
            {
                Url = url,
                Method = HttpMethod.GET
            };
            var stream = httpClient.GetImageStream(parameter);
            return Image.FromStream(stream);
        }

        /// <summary>
        /// 登录网页版飞信
        /// </summary>
        /// <param name="userName">手机号、飞信号或邮箱</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public Result Login(string userName, string password, string code)
        {
            var dic = new Dictionary<string, string>
            {
                {"UserName", userName},
                {"Pwd", password},
                {"OnlineStatus", "400"},
                {"AccountType", "1"},
                {"Ccp", code}
            };

            var parameter = new HttpParameter
            {
                Url = "https://webim.feixin.10086.cn/WebIM/Login.aspx",
                Host = "webim.feixin.10086.cn",
                Origin = "https://webim.feixin.10086.cn",
                Referer = "https://webim.feixin.10086.cn/loginform.aspx",
                Method = HttpMethod.POST,
                Data = dic
            };

            var json = httpClient.Request(parameter);
            var result = JsonConverter.FromJson<Result>(json);
            return result;
        }

        /// <summary>
        /// 获取个人资料
        /// </summary>
        /// <returns></returns>
        public PersonalInfo GetPersonalInfo()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetPersonalInfo.aspx?Version=0&ssid={0}", GetSsid());
            var parameter = new HttpParameter()
            {
                Url=url,
                Referer = "https://webim.feixin.10086.cn/main.aspx",
                Method =HttpMethod.GET
            };

            var json = httpClient.Request(parameter);
            var info = JsonConverter.FromJson<PersonalInfo>(json);
            return info;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="from">发送人的uid</param>
        /// <param name="to">接收人的uid(多个以“,”隔开)</param>
        /// <param name="msg">短信内容</param>
        /// <returns></returns>
        public Result Send(string from,string to,string msg)
        {
            var dic = new Dictionary<string, string>
            {
                {"UserName", from},
                {"Message",Uri.EscapeDataString(msg)},
                {"Receivers", to},
                {"ssid", GetSsid()}
            };

            var parameter = new HttpParameter()
            {
                Url = "https://webim.feixin.10086.cn/content/WebIM/SendSMS.aspx?Version=160",
                Host = "webim.feixin.10086.cn",
                Origin = "https://webim.feixin.10086.cn",
                Referer = "https://webim.feixin.10086.cn/content/freeSms.htm?tabIndex=0",
                Method = HttpMethod.POST,
                Data = dic
            };

            var json = httpClient.Request(parameter);
            var result = JsonConverter.FromJson<Result>(json);
            return result;
        }

        /// <summary>
        /// 获得联系人信息
        /// </summary>
        /// <returns></returns>
        public ConnectInfo GetConnectInfo()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetConnect.aspx?Version=1&ssid={0}", GetSsid());
            var parameter = new HttpParameter()
            {
                Url = url,
                Referer = "https://webim.feixin.10086.cn/main.aspx",
                Method = HttpMethod.GET
            };

            var json = httpClient.Request(parameter);
            var info = JsonConverter.FromJson<ConnectInfo>(json);
            return info;
        }

        /// <summary>
        /// 获得会话编号
        /// </summary>
        /// <returns></returns>
        private string GetSsid()
        {
            var lst=httpClient.GetAllCookies();
            var cookie = lst.Single(c => c.Name == "webim_sessionid");
            return cookie.Value;
        }

    }
}
更多源代码请访问:https://lyh.codeplex.com/ 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值