1. 程式人生 > >C# 調用微信接口上傳素材和發送圖文消息

C# 調用微信接口上傳素材和發送圖文消息

context puts out odin app bin utf8 light while

using Common;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;

     /// <summary>
        /// 調用微信接口憑證access_token
        /// </summary>
        private static string test_access_token
        {
            get
            {
                return "XXXXXXXXXXXX";
            }
        }

        /// <summary>
        /// 新增其他類型永久素材,返回值{"media_id":"eZh1QTjGGSyE-i9k8uHZqrd5LpHfYBsKtUrSfnjf8k0",
        /// "url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/gHnmqhvpvh5HoibMEcGEAK4eAKvIR18kuKoXbjCiaRa1p1WTBgicYMDvqkjTadib21KUWYpibzfuXj6ibRw8ibw\/0?wx_fmt=png"}
        /// </summary>
        /// <param name="url">目標地址</param>
        /// <param name="path">圖片物理文件路徑</param>
        /// <returns></returns>
        public string add_material()
        {
            //圖片(image): 2M,支持bmp/png/jpeg/jpg/gif格式
            //語音(voice):2M,播放長度不超過60s,mp3/wma/wav/amr格式
            //視頻(video):10MB,支持MP4格式
            //縮略圖(thumb):64KB,支持JPG格式
                        
            var file = Request.Files[0];
            string fileName = file.FileName;
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", test_access_token, "image");
            
            //讀取上載文件流
            System.IO.Stream fileStream = file.InputStream;
            byte[] fileByte = new byte[fileStream.Length];
            fileStream.Read(fileByte, 0, fileByte.Length);

            // 設置參數
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

            //請求頭部信息
            StringBuilder sbHeader =
                new StringBuilder(
                    string.Format(
                        "Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n",
                        fileName));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());

            Stream postStream = request.GetRequestStream();
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(fileByte, 0, fileByte.Length);
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();

            //發送請求並獲取相應回應數據
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            string content = sr.ReadToEnd();
            return content;
        }

  

  

     /// <summary>
        /// 上傳圖文消息素材,返回格式:{"type":"news","media_id":"mF1J9boYDAQlYew4wbvbxQKMBkLPa1WzhGbDW7FVak","created_at":1391857799}
        /// </summary>
        /// <returns></returns>
        public string add_news()
        {
            var news = "{\"articles\":[{\"thumb_media_id\":\"mF1J9boYDAQlYew4wbvbxTgoKle16WjhsxuwhV9ZtQ\",\"author\":\"PDF\",\"title\":\"車行易.違章查詢\",\"content_source_url\":\"www.qq.com\",\"content\":\"\",\"digest\":\"為車主朋友們提供優質讓人滿意的服務\",\"show_cover_pic\":1}]}";
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");
            WriteLog.WriteLogToFile("add_news RESULT:" + result);
            return result;
        }

  

     /// <summary>
        /// 預覽接口(發送給指定的openId)
        /// </summary>
        /// <returns></returns>
        public string preview()
        {
            //說明:media_id值來自add_news接口返回值中的media_id值
            var news = "{\"touser\":\"oTD55jj52uIhOObiwrxCjjrCl9g\",\"mpnews\":{\"media_id\":\"mF1J9boYDAQlYew4wbbxQKMBkLPa1WzwhGbDW7FVak\"},\"msgtype\":\"mpnews\"}";
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");
            WriteLog.WriteLogToFile("preview RESULT:" + result);
            return result;
        }

  

     /// <summary>
        /// 根據標簽進行群發【警告,謹慎調用】
        /// </summary>
        /// <returns></returns>
        public string sendall()
        {
            //說明:media_id值來自add_news接口返回值中的media_id值
            var news = "{\"filter\":{\"is_to_all\":false,\"tag_id\":215},\"mpnews\":{\"media_id\":\"mF1J9boYDAQlYew4wbbxQKMBkLPa1WzwhGbDW7FVak\"},\"msgtype\":\"mpnews\",\"send_ignore_reprint\":1,\"clientmsgid\":\"20171107\"}";
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");
            WriteLog.WriteLogToFile("sendall RESULT:" + result);
            return result;
        }

  

     /// <summary>
        /// 獲取永久素材的列表   
        /// </summary>
        /// <returns></returns>
        public string batchget_material()
        {
            var news = "{\"type\":\"news\",\"offset\":0,\"count\":3}";
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");
            WriteLog.WriteLogToFile("batchget_material RESULT:" + result);
            return result;
        }

  

     /// <summary>
        /// 獲取永久素材詳細
        /// </summary>
        /// <returns></returns>
        public string get_material()
        {
            var news = "{\"media_id\":\"mF1J9boYDAQlYew4wbvbxQKMBkLa1WzwhGbDW7FVak\"}";
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Post(newsUrl, news, null, "applicaion/json");
            WriteLog.WriteLogToFile("get_material RESULT:" + result);
            return result;
        }

  

     /// <summary>
        /// 獲取微信用戶分組(用戶標記)
        /// </summary>
        /// <returns></returns>
        public string gettags()
        {
            var newsUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={0}";
            newsUrl = string.Format(newsUrl, test_access_token);
            var result = HttpHelper.Get(newsUrl);
            WriteLog.WriteLogToFile("tags RESULT:" + result);
            return result;
        }

  

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

namespace Common
{
    public class HttpHelper
    {
        /// <summary>
        /// 發起GET請求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Get(string url)
        {
            var webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));

            webReq.KeepAlive = false;
            webReq.Method = "GET";
            webReq.Timeout = 20000;
            webReq.ProtocolVersion = HttpVersion.Version11;
            webReq.ContentType = "application/x-www-form-urlencoded";

            var response = (HttpWebResponse)webReq.GetResponse();
            var readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            Char[] read = new Char[256];
            var count = readStream.Read(read, 0, 256);
            var result = string.Empty;
            while (count > 0)
            {
                result += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            response.Close();
            readStream.Close();
            return result;
        }


        /// <summary>
        ///  發起POST請求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string Post(string url, string postData, Dictionary<string, string> headers = null, string contentType = null)
        {
            var webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            byte[] bytes = encode.GetBytes(postData);

            webReq.KeepAlive = false;
            webReq.Method = "POST";
            webReq.Timeout = 20000;
            webReq.ProtocolVersion = HttpVersion.Version11;
            if (contentType == null)
                webReq.ContentType = "application/x-www-form-urlencoded";
            else
                webReq.ContentType = contentType;

            webReq.ContentLength = bytes.Length;
            webReq.UserAgent = "Mozilla/5.0";
            if (headers != null)
            {
                foreach (var header in headers)
                    webReq.Headers.Add(header.Key, header.Value);
            }

            Stream outStream = webReq.GetRequestStream();
            outStream.Write(bytes, 0, bytes.Length);
            outStream.Close();

            var response = (HttpWebResponse)webReq.GetResponse();
            var readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            Char[] read = new Char[256];
            var count = readStream.Read(read, 0, 256);
            var result = string.Empty;
            while (count > 0)
            {
                result += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            response.Close();
            readStream.Close();
            return result;
        }


        /// <summary>
        /// 獲取Post值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetPostValue(HttpContext context)
        {
            System.IO.Stream s = context.Request.InputStream;
            int count = 0;
            byte[] buffer = new byte[s.Length];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            return builder.ToString();
        }
    }
}

  

C# 調用微信接口上傳素材和發送圖文消息