C# 实现QQ微博分享接口应用

编程入门 行业动态 更新时间:2024-10-10 21:27:10

C# 实现QQ微博分享<a href=https://www.elefans.com/category/jswz/34/1771365.html style=接口应用"/>

C# 实现QQ微博分享接口应用

C# 实现QQ微博分享接口应用

2012-11-01  来自:cnblogs  字体大小:【 大  中  小】
  • 摘要:QQ微博在营销领域越来越受青睐了,这里面集成了很多非常有用的接口,像是邮件分享、空间分享、QQ分享、微信分享等,本文介绍C# 实现QQ微博分享接口应用 。
QQ微博在营销领域越来越受青睐了,这里面集成了很多非常有用的接口,像是邮件分享、空间分享、QQ分享、微信分享等。这相对于传统的直接模拟协议,登录邮箱等方式进行邮件发送甚至更有效。所有这些都没什么技术难度,所以实现起来是很简单的。如果在开发过程中遇到了些困难的话,可能是多线程的把握吧!

在这样一些营销类项目之中,绝大多数都设计了多账号切换操作,这使得整个架构控制起来异常繁琐。对于多线程功底稍差的人来说,加上UI设计的搭配,简直就是地狱般的折磨。项目实战开发最需要的就是经验的积累,平时练习的时候就得多为下一次开发做技术上的准备,否则一旦开始就会显得很吃力耗时。

上图只是我一个项目里的一张截图,我们要说的功能大致就是上面图中所表现出来的。但是这里,只做一个功能实现描述,不作项目完整实现。

步骤:

1.基础功能的类准备

1.处理xml的xmlHelper,2.处理Http模拟的HttpHelper 3.处理QQ登录验证密码MD5加密的QQMd5 4.其他一些Util功能实现类

所有这些类我会稍后放在文章附件里。

2.创建Account用户资料类[登录]

下面的这个类我只是给各位一个例子,可以展开看一下,但是还是要自己依据自己情况具体分析。

  public class Account{#region 属性/// <summary>/// 帐号登录的顺序号码/// </summary>       public int ID { get; set; }/// <summary>/// QQ帐号/// </summary>       public string Uin { get; set; }public List<string> Froms { get; set; }public string Mail { get; set; }/// <summary>/// 昵称/// </summary>       public string Nick { get; set; }/// <summary>/// 我的首页/// </summary>       public string MyPage { get; set; }/// <summary>/// QQ未加密的密码/// </summary>       public string Password { get; set; }/// <summary>/// QQ加密密码/// </summary>       public string P { get; set; }/// <summary>/// QQ微博的Cookie容器/// </summary>       public CookieContainer WeiboCookieContainer { get; set; }/// <summary>/// QQ微博的Cookie字符/// </summary>       public string WeiboCookieString { get; set; }/// <summary>/// QQ微博对应的Sid/// </summary>       public string Sid { get; set; }/// <summary>/// 目前帐号的状态/// </summary>       public Status CurrentStatus { get; set; }/// <summary>/// 帐号类型/// </summary>       public AccountStyle AccountStyle { get; set; }/// <summary>/// 是否需要验证码/// </summary>       public bool NeedVerify { get; set; }/// <summary>/// 验证码图片/// </summary>       public Image VerifyImage { get; set; }/// <summary>/// 验证码字符串/// </summary>       public string VerifyCode { get; set; }#endregion#region 构造函数public Account(){Init();}public Account(string uin, string password){this.Uin = uin;this.Password = password;Init();}#endregion#region 私有函数private void Init(){this.WeiboCookieContainer = new CookieContainer();this.CurrentStatus = Status.Unlogin;this.AccountStyle = AccountStyle.Unknow;}#endregion}}
上面的那些都不是关键,我们要做的第一步还是完成“判断验证码>输入验证码(如果有的话)>登录”,除了要我完成Http模拟请求之外还要从返回的结果中提取有用的信息。
  #region 登录过程/// <summary>/// 判断登录微博是否需要验证码/// </summary>/// <returns></returns>       public bool CheckVerify_Weibo(ref string vctype){string url = "", html;url = string.Format("={0}@qq&appid=46000101&ptlang=2052&r={1}", this.Uin,new Random().NextDouble());html = HttpHelper.GetHtml(url, this.WeiboCookieContainer);if (string.IsNullOrEmpty(html)) return false;string pattern = @"ptui_checkVC\('(?'need'[^']*)','(?'vctype'[^']*)','[^']*'\);";string need = html.Match(pattern, "need");vctype = html.Match(pattern, "vctype");if (need == "1"){this.CurrentStatus = Status.NeedVerify;this.VerifyImage= this.GetImage();return true;}else{this.CurrentStatus = Status.Unlogin;this.VerifyCode = vctype;return false;}}/// <summary>/// 获取显示验证码/// </summary>/// <returns></returns>       public Image GetImage(){string url = string.Format("=46000101&r=0.38706237439032276&uin={0}@qq", this.Uin);Stream stream = HttpHelper.GetStream(url, this.WeiboCookieContainer);Image image = Image.FromStream(stream);return image;}/// <summary>/// 登录/// </summary>/// <returns></returns>       public string Login(){string uin,password,verifyCode;uin = this.Uin;password = this.Password;verifyCode = this.VerifyCode;string html="";if (string.IsNullOrEmpty(uin) || string.IsNullOrEmpty(password))throw new Exception("没有添加帐号,或帐号的密码为空");if (string.IsNullOrEmpty(verifyCode))throw new Exception("验证码为空,无法继续登录");this.P = QQMd5.Encrypt(uin,password,verifyCode);string url = string.Format("=2052&u={0}@qq&p={1}&verifycode={2}&low_login_enable=1&low_login_hour=720&css=.css&aid=46000101&mibao_css=m_weibo&u1=http%3A%2F%2Ft.qq&ptredirect=1&h=1&from_ui=1&dumy=&fp=loginerroralert&action=7-9-1381063&g=1&t=1&dummy=",uin, this.P, verifyCode);html = HttpHelper.GetHtml(url, this.WeiboCookieContainer);if (html.Contains("您输入的验证码不正确,请重新输入"))throw new Exception(string.Format("验证码输入不正确"));else if (html.Contains("您输入的帐号或密码不正确,请重新输入"))throw new Exception(string.Format("帐号或密码不正确"));else if (html.Contains("登录成功")){html = string.Format("登录成功");this.CurrentStatus = Status.Login;}return html;}
由于本人不太喜欢用已经做好的轮子去用Json处理类(比如Newtonsoft),所以一般处理都是用的正则表达式,各位也可以自己用JsonObejct类去处理,不用跟我一样用正则这种笨方法。
3. 获取必要信息[提取微博数据]

 

  public string GetInfo(){string html="";try{string url = "";html = HttpHelper.GetHtml(url, this.WeiboCookieContainer);string pattern = @"href=""(?'mypage'[^""]*)""><u>首页";string mypage = html.Match(pattern, "mypage");this.MyPage = mypage;if (string.IsNullOrEmpty(mypage)) throw new Exception("获取微博话题列表失败");pattern = @"boss=""btnWideSideMyNick"">(?'nick'[^<]*)</a>";string nick = html.Match(pattern, "nick");if (html.Contains("立即开通")) this.CurrentStatus = Status.NotRegist;this.Nick = nick;html = "成功获取微博话题列表";}catch(Exception e){html = e.Message;}return html;}public List<FriendInfo> GetQQFriendList(){string account = ""; string r = "1351620387406";account = this.MyPage.ToLower().Replace("/", "");string url = string.Format(".php?account={0}&r={1}&apiType=8&apiHost=;_r={1}",account,r);HttpHelper.Referer = this.MyPage;HttpHelper.ExtendHeadData = string.Format("rf:{0}", this.MyPage);string html = HttpHelper.GetHtml(url, this.WeiboCookieContainer);HttpHelper.Referer = "";HttpHelper.ExtendHeadData = "";Regex regex = new Regex(@"(""sortId"":(?'sordId'[^""]+),)*""name"":""(?'name'[^""]+)"",(""groupId"":(?'groupId'[^,]+),)*(""member"":(?'member'\[[^\]]*\]))*", RegexOptions.IgnoreCase);MatchCollection matches = regex.Matches(html);List<FriendInfo> list = new List<FriendInfo>();for (int i = 0; i < matches.Count; i++){string sortId = matches[i].Groups["sortId"].Value;string name = matches[i].Groups["name"].Value;string groupId = matches[i].Groups["groupId"].Value;string member = matches[i].Groups["member"].Value;Regex memberRegex = new Regex(@"""qq"":""(?'qq'[^""]*)"",""pic"":null,""nick"":""(?'nick'[^""]*)""", RegexOptions.IgnoreCase);MatchCollection memberMatches = memberRegex.Matches(member);for (int j = 0; j < memberMatches.Count; j++){string qq = memberMatches[j].Groups["qq"].Value;string nick = memberMatches[j].Groups["nick"].Value;FriendInfo friendInfo = new FriendInfo();friendInfo.Uin = qq;friendInfo.Name = WebQQ.Converter.Unicode_js_1(name);friendInfo.Nick = WebQQ.Converter.Unicode_js_1(nick);if (friendInfo.Name == "最近联系人") continue;friendInfo.SortId = sortId;friendInfo.GroupId = groupId;friendInfo.QQ = Uin;list.Add(friendInfo);}}return list;}

上面这一步不一定是必须的,但是没有这一步,我们后面所要实现的功能就会很困难。包括发送分享给QQ,这一步,因为要发送的QQ好友的号码不是真正的号码,而是一个系统随机生成的好友序列号的MD5加密字段。因此,我们也无法用于分享QQ给陌生人号码。

4.分享

  #region 分享public string MailShare(string shareId,string toList,string subject,string reason){string url, html;url = ".php";HttpHelper.ExtendHeadData =string.Format("rf:{0}",this.MyPage);HttpHelper.Referer = ".html";string mail = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(this.Mail));string maillist = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(toList));subject = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(subject));reason = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(reason));string postString = string.Format("mailAddr={0}&mlist={1}&subject={2}&body={3}&reason={4}&apiType=8&apiHost=",mail,toList,subject,shareId,reason);html = HttpHelper.GetHtml(url, postString, this.WeiboCookieContainer);HttpHelper.Referer = "";return html;}public string ShareQZone(string shareId, string reason){string url = ".php";HttpHelper.ExtendHeadData = string.Format("rf:{0}", this.MyPage);HttpHelper.Referer = ".html";reason = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(reason));string postString = string.Format("id={0}&reason={1}&apiType=8&apiHost=",shareId,reason);string html = HttpHelper.GetHtml(url, postString, this.WeiboCookieContainer);HttpHelper.Referer = "";return html;}public string ShareMsg(string shareId,string uins,string group){string url = ".php";string postString = string.Format("id={0}&uins={1}&group={2}&apiType=8&apiHost=",shareId,uins,group);HttpHelper.ExtendHeadData = string.Format("rf:{0}", this.MyPage);HttpHelper.Referer = ".html";string html = HttpHelper.GetHtml(url, postString, this.WeiboCookieContainer);HttpHelper.Referer = "";return html;}public string Pm_Mgr(string content,string target){string url = ".php";content = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(content));target = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(target));string postString = string.Format("source=&ptid=&roomid=&content={0}&fid=&arturl=&murl=&target={1}&func=send&ef=js&pmlang=zh_CN&apiType=8&apiHost=",content,target);//wuwenjun20102008,niefeng101           string html = HttpHelper.GetHtml(url,postString, this.WeiboCookieContainer);return html;}
 

更多推荐

C# 实现QQ微博分享接口应用

本文发布于:2024-03-09 18:37:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1725755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   QQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!