1. 程式人生 > >c# 微信模擬登入抓取公共帳號傳送圖文資訊

c# 微信模擬登入抓取公共帳號傳送圖文資訊

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