c# 微信模拟登录抓取公共帐号发送图文信息

转载 2016年08月28日 16:05:38

原文链接:http://blog.csdn.net/panzhixin2012/article/details/23845263


using System;  
using System.Collections.Generic;  
using System.Web;  
using System.IO;  
using System.Net;  
using System.Security.Cryptography;  
using System.Text;  
using System.Security.Cryptography.X509Certificates;  
using System.Net.Security;  
using System.Runtime.Serialization;  
using LitJson;  
using System.Drawing;  
using System.Drawing.Imaging;  
using Dejun.DataProvider;  
using Dejun.DataProvider.Table;  
using Dejun.DataProvider.Sql2005;  
  
/// <summary>  
/// weixinLogin 的摘要说明  
/// </summary>  
public class weixinLogin  
{  
    public weixinLogin()  
    {  
        //  
        // TODO: 在此处添加构造函数逻辑  
        //  
    }  
    public static CookieContainer cc = new CookieContainer();  
    public static string Tonken = "";  
  
  
    public static string Gethtml(string username,string pwd)  
    {  
        string html = "";  
  
        ExecLogin("http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN", username, pwd);  
  
        html = Getweixin("https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/list&action=history&begin=0&count=1000&token=" + Tonken + "&lang=zh_CN");  
        if (html.IndexOf("wx.cgiData") > -1)  
        {  
            html = html.Replace("\r", "").Replace("\n", "").Replace("<br>","") ;  
            html = SubString1(html, "list : (", ").msg_item");  
         
        JsonData json = JsonMapper.ToObject(html);  
        JsonData jditems = json["msg_item"];  
        int itemcount = jditems.Count;  
        foreach( JsonData item in jditems )  
        {  
            string tuwen = item.ToJson();  
              
            string content = "";  
            string title = "";  
            if (tuwen.IndexOf("content_url") > -1)  
            {  
                string url = (String)item["content_url"];//获取url  
                 title = (String)item["title"];//信息标题  
                string str = Getweixin(url);//获取单条信息内容页  
                content = SubString1(str, "<div id=\"page-content\" class=\"page-content\">", "<p class=\"page-toolbar\">"); //信息内容  
                if (content.IndexOf("<div class=\"media\" id=\"media\">") > -1)  
                {  
                    string fristpic = SubString1(content, "<div class=\"media\" id=\"media\">", "<div class=\"text\">");  
                    string tihuan = SubString1(fristpic, "<img", "data-src");  
                    content = content.Replace(tihuan, "");  
                }  
                 
                content = content.Replace("data-src", "src");  
                content = content.Replace("<img", "<img width='470'");  
                 
  
            }  
            else  
            {  
                content = (String)item["content"];  
                title = content.Substring(0,8);  
              //  title = "新闻内容消息";  
            }  
            int msgid = (Int32)item["id"];  
           // int fileid = (Int32)item["file_id"];  
           // string picurl = "https://mp.weixin.qq.com/cgi-bin/getimgdata?&token="+Tonken+"&lang=zh_CN&mode=small&source=mass&msgid="+msgid+"&fileId="+fileid+"";  //列表图片地址  
      //  https://mp.weixin.qq.com/cgi-bin/getimgdata?&token=569626606&lang=zh_CN&mode=small&source=mass&msgid=29300&fileId=200050665  
             
             
              
            string addname = (String)item["nick_name"];  
            string fakeid = (String)item["fakeid"]; //fakeid id  
            int mid=(Int32)item["id"];//信息id  
              
            News wxcondition = new News();  
            News value = new News();  
            wxcondition.TypeID = 24;  
            wxcondition.Maps = Convert.ToString(mid);  
            wxcondition.SaiShi = fakeid;  
            value = TableOperate<News>.GetRowData(wxcondition);  
            if (value.ID > 0)  
            {  
  
            }  
            else  
            {  
  
              //  string pic = GetVerifyCode(picurl, "weixinpic");//保存列表图片  
                News condition = new News();  
                condition.Maps = Convert.ToString(mid);  
                condition.SaiShi = fakeid;  
                condition.Title = title;  
                condition.Content = content;  
              
                condition.PublishTime = DateTime.Now;  
                condition.TypeID = 24;  
                condition.AddName = addname;  
                condition.State = 1;  
                TableOperate<News>.Insert(condition);  
  
  
            }  
  
  
  
  
        }  
  
        return "1";  
        }  
  
  
        return "0";  
    }  
     
  
    public static string ExecLogin(string url, string name, string pass)//登陆微信公众平台函数  
    {  
  
        string result = "";  
  
        string password = GetMd5Str32(pass).ToUpper();  
  
        string padata = "username=" + name + "&pwd=" + password + "&imgcode=&f=json";  
  
        //g url = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//请求登录的URL  
  
        try  
        {  
  
          //  CookieContainer cc = new CookieContainer();//接收缓存  
  
            byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 转化  
  
            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);  //新建一个WebRequest对象用来请求或者响应url  
  
            webRequest2.CookieContainer = cc;                                      //保存cookie    
  
            webRequest2.Method = "POST";                                          //请求方式是POST  
  
            webRequest2.ContentType = "application/x-www-form-urlencoded";       //请求的内容格式为application/x-www-form-urlencoded  
  
            webRequest2.ContentLength = byteArray.Length;  
  
            webRequest2.Referer = "https://mp.weixin.qq.com/";  
  
            Stream newStream = webRequest2.GetRequestStream();           //返回用于将数据写入 Internet 资源的 Stream。  
  
            // Send the data.  
  
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数  
  
            newStream.Close();  
  
            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();  
  
            StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);  
  
            string text2 = sr2.ReadToEnd();  
  
  
            result = text2;  
            if (result.Length > 0)  
            {  
                //cookie  
  
                result =checkStr(SubString1(result, "lang=zh_CN&token=", "ShowVerifyCode").Replace(",","").Replace("\"",""));  
                
               // result = Tonken;  
                Tonken = result.Replace("\n","");  
            }  
  
  
        }  
  
        catch (Exception ex)  
        {  
  
            throw new Exception(ex.StackTrace);  
  
        }  
  
        return result;  
  
    }  
    public static string Getweixin(string url)//抓取新闻函数  
    {  
      //  string Url = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/list&action=history&begin=0&count=10&token=" + Tonken + "&lang=zh_CN";  
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);  
        myRequest.CookieContainer = cc;  
        myRequest.Method = "GET";  
        myRequest.Proxy = null;  
        myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0";  
        myRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.8,zh-hk;q=0.6,ja;q=0.4,zh;q=0.2");  
        myRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";  
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };  
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  
        string str = new System.IO.StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")).ReadToEnd();  
        return str;  
    }  
    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)  
    {  
        return true; //总是接受  
    }  
  
    public static string SubString1(string sourceStr, string beginStr, string endStr)  
    {  
        int begin = sourceStr.IndexOf(beginStr);  
        if (begin == -1) return "";  
        int end = sourceStr.IndexOf(endStr, begin + beginStr.Length);  
        if (end == -1 && end == begin + beginStr.Length) return "";  
  
        return sourceStr.Substring(begin + beginStr.Length, end - begin - beginStr.Length);  
  
    }  
  
    public static string GetVerifyCode(string sourceUrl, string saveVirtualAddress)//下载图片  
    {  
         
  
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sourceUrl);  
        req.CookieContainer = cc;  
        using (WebResponse res = req.GetResponse())  
        {  
            System.Drawing.Image bmp = Bitmap.FromStream(res.GetResponseStream());  
            string fn = System.IO.Path.Combine(SysConfig.ApplicationPath+ saveVirtualAddress.TrimEnd('/') + "/", Guid.NewGuid().ToString() + ".jpg");  
            string address = HttpContext.Current.Server.MapPath(fn);  
            bmp.Save(address, ImageFormat.Jpeg);  
            return fn;  
        }  
    }  
  
    public static string GetMd5Str32(string str) //MD5摘要算法  
    {  
  
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();  
  
        // Convert the input string to a byte array and compute the hash.    
  
        char[] temp = str.ToCharArray();  
  
        byte[] buf = new byte[temp.Length];  
  
        for (int i = 0; i < temp.Length; i++)  
        {  
  
            buf[i] = (byte)temp[i];  
  
        }  
  
        byte[] data = md5Hasher.ComputeHash(buf);  
  
        // Create a new Stringbuilder to collect the bytes    
  
        // and create a string.    
  
        StringBuilder sBuilder = new StringBuilder();  
  
        // Loop through each byte of the hashed data     
  
        // and format each one as a hexadecimal string.    
  
        for (int i = 0; i < data.Length; i++)  
        {  
  
            sBuilder.Append(data[i].ToString("x2"));  
  
        }  
  
        // Return the hexadecimal string.    
  
        return sBuilder.ToString();  
  
    }  
    public static string checkStr(string html)  
    {  
        System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);  
        html = regex1.Replace(html, ""); //过滤<script></script>标记   
        html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性   
        html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件   
        html = regex4.Replace(html, ""); //过滤iframe   
        html = regex5.Replace(html, ""); //过滤frameset   
        html = regex6.Replace(html, ""); //过滤frameset   
        html = regex7.Replace(html, ""); //过滤frameset   
        html = regex8.Replace(html, ""); //过滤frameset   
        html = regex9.Replace(html, "");  
        html = html.Replace(" ", "");  
        html = html.Replace("</strong>", "");  
        html = html.Replace("<strong>", "");  
        return html;  
    }   
}  




相关文章推荐

c# 微信模拟登录抓取公共帐号发送图文信息

using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Net;...

模拟微信发送图文信息

  • 2014年06月16日 14:26
  • 2.44MB
  • 下载

【java、Spring MVC】微信公共帐号开发_开始接入

第一步:填写服务器配置 兵车未动,粮草先行,首先你得有台服务器,80端口未被占用~ 想必这里的api大家都看过的 说明:URL填写你服务器80端口的URL,比如楼主的是(http...
  • xb12369
  • xb12369
  • 2015年05月12日 17:02
  • 10383

微信公共帐号代码java

  • 2017年03月31日 14:49
  • 4.25MB
  • 下载

C#微信企业号群发图文信息

  • 2017年11月06日 11:24
  • 4.04MB
  • 下载

java微信公共帐号支付(JS支付)

最近公司需要开发微信商城,然后各种作微信的东西啊,各种看API,头大,这里先记录下来以免后面忘记了~ 首先是微信提供的api地址:https://pay.weixin.qq.com/wiki/doc/...
  • xb12369
  • xb12369
  • 2016年07月10日 00:25
  • 5543

豆瓣搜索—微信公共平台测试帐号申请

最近稍微有些时间,打算折腾下python微信公共平台开发,豆瓣书籍基本功能是输入需要查询的书籍名称,然后会返回从豆瓣上查询到的该书籍相关的信息。 首先本篇主要说一下如何申请公共平台测试帐号,由于个人帐...
  • lf8289
  • lf8289
  • 2015年05月05日 14:24
  • 632

[037] 微信公众帐号开发教程第13篇-图文消息全攻略

引言及内容概要 已经有几位读者抱怨“柳峰只用到文本消息作为示例,从来不提图文消息,都不知道图文消息该如何使用”,好吧,我错了,原本以为把基础API封装完、框架搭建好,再给出一个文本消息的使用示例,大...
  • lyq8479
  • lyq8479
  • 2013年07月25日 22:35
  • 180826

微信公众帐号开发教程第13篇-图文消息全攻略

引言及内容概要 已经有几位读者抱怨“柳峰只用到文本消息作为示例,从来不提图文消息,都不知道图文消息该如何使用”,好吧,我错了,原本以为把基础API封装完、框架搭建好,再给出一个文本消息的使用示例...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c# 微信模拟登录抓取公共帐号发送图文信息
举报原因:
原因补充:

(最多只允许输入30个字)