首页 > 代码库 > asp.net MVC 抓取微信文章数据(正文)

asp.net MVC 抓取微信文章数据(正文)

1.抓微信的正文主要是调用第三方的接口(https://market.aliyun.com/products/56928004/cmapi012134.html)

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace QBSqlServer.GSDataAPIs.GetHtml
{
    public class WeChatPublicNumberQueryAPI
    {
        private const String host = "https://ali-weixin.showapi.com";
        private const String path = "/582-9";
        private const String method = "GET";
        private const String appcode = "你自己的appcode";

        public static Root GetWeChathtml(string title)
        {
            string outhtml = string.Empty;
            string t = System.Web.HttpUtility.UrlEncode(title);
            //String querys = "needComment=0&needContent=1&url=url";
            String querys = "needContent=1&url=" + t;
            String bodys = "";
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            if (host.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            if (0 < bodys.Length)
            {
                byte[] data =http://www.mamicode.com/ Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            Console.WriteLine(httpResponse.StatusCode);
            Console.WriteLine(httpResponse.Method);
            Console.WriteLine(httpResponse.Headers);
            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            string strResult = reader.ReadToEnd();
            Root jobInfoList = JsonConvert.DeserializeObject<Root>(strResult);
            Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine("\n");
            return jobInfoList;
        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }

    public class Showapi_res_body
    {
        /// <summary>
        /// 
        /// </summary>
        public string newUrl { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string date { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string weixinNum { get; set; }
        /// <summary>
        /// 这是正文的html
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ret_code { get; set; }
        /// <summary>
        /// 秀场|中国品牌ELLASSAY米兰时装周首秀!
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string contentImg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string userLogo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string oldUrl { get; set; }
        /// <summary>
        /// 徐峰立
        /// </summary>
        public string userName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string read_num { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string like_num { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string userLogo_code { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public string showapi_res_code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string showapi_res_error { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Showapi_res_body showapi_res_body { get; set; }
    }
}

 

asp.net MVC 抓取微信文章数据(正文)