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;...

微信公众帐号消息帐号

using System;   using System.Collections;   using System.Configuration;   using System.Data; ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

微信公众号,微信模版发送信息(java)

微信公众平台授权登录获取用户openid。 微信公众平台地址:https://mp.weixin.qq.com/ 1:模板配置 2:发送模板的接口文档 3:微信回调地址域名配置 4:注意:按...

使用httpclient模拟登录京东帐户并抓取帐户信息

这个,前几天有个人出2000软妹币让我做这个功能,应该是风控系统需要吧,我就鼓捣起来,先分析了一下京东的请求,发现未加密密码,呵呵呵呵呵呵,故意的吧喂,那我就不客气哦上代码: 主攻登录的:packa...

抓取微信公共号的推送内容

如何抓取微信公共号的最新推送文章,快来看看吧

Salesforce 发送公共信息

public class ChatterSender { /* * 发送chatter消息公共类 * userId:@对象 * text:消息文本 * ...
  • lovoo
  • lovoo
  • 2016-07-04 10:56
  • 242

向微信公众号发送消息

向微信公众号发送文本消息        首先说下需求,在项目中有时需要时时的知道线上系统的稳定性,防止恶意刷接口,因此就有了线上接口监控系统,时时统计接口在某个时段内调用次数。调用次数超过阀值时进行报...

[微信公众号]php版模拟登录,向指定用户推送消息目前

[微信公众号]php版模拟登录,向指定用户推送消息目前微信公众号没有开放推送接口。模拟登录即模拟用户登录,可以查看用户消息,回复消息等。微信官方未表态是否禁止,大量使用可能会导致封号等,谨慎使用!以下...

模拟微信登录

#coding:utf-8 """ 微信公众平台管理界面 """ import requests import hashlib import random import ttlsa import js...

简单的http抓包(微信公众网页登录模拟)

一、准备工具: 系统:XP 浏览器:IE8 抓包工具:HttpWatch(它可以查看url请求的数据包) 二、抓包思路: 浏览器上的任何获取数据的方式都符合http协...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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