原文链接: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;
}
}