国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#微信公眾平臺開發—高級群發接口

2019-11-17 02:45:20
字體:
來源:轉載
供稿:網友

C#微信公眾平臺開發—高級群發接口

涉及access_token的獲取請參考《C#微信公眾平臺開發—access_token的獲取存儲與更新》

一、為了實現高級群發功能,需要解決的問題

1、通過微信接口上傳圖文消息素材時,Json中的圖片不是url而是media_id,如何通過微信接口上傳圖片并獲取圖片的media_id?

2、圖片存儲在什么地方,如何獲取?

二、實現步驟,以根據OpenID列表群發圖文消息為例

1、準備數據

我把數據存儲在數據庫中,ImgUrl字段是圖片在服務器上的相對路徑(這里的服務器是自己的服務器,不是微信的哦)。

從數據庫中獲取數據放到DataTable中:

DataTable dt = ImgItemDal.GetImgItemTable(loginUser.OrgID, data);
View Code

2、通過微信接口上傳圖片,返回圖片的media_id

取ImgUrl字段數據,通過MapPath方法獲取圖片在服務器上的物理地址,用FileStream類讀取圖片,并上傳給微信

HTTP上傳文件代碼(HttPRequestUtil類):

/// <summary>/// Http上傳文件/// </summary>public static string HttpUploadFile(string url, string path){    // 設置參數    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 = "multapplication/octet-stream/r/n/r/n", fileName));    byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);    byte[] bArr = new byte[fs.Length];    fs.Read(bArr, 0, bArr.Length);    fs.Close();    Stream postStream = request.GetRequestStream();    postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);    postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);    postStream.Write(bArr, 0, bArr.Length);    postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);    postStream.Close();    //發送請求并獲取相應回應數據    HttpWebResponse response = request.GetResponse() as HttpWebResponse;    //直到request.GetResponse()程序才開始向目標網頁發送Post請求    Stream instream = response.GetResponseStream();    StreamReader sr = new StreamReader(instream, Encoding.UTF8);    //返回結果網頁(html)代碼    string content = sr.ReadToEnd();    return content;}
View Code

請求微信接口,上傳圖片,返回media_id(WXApi類):

/// <summary>/// 上傳媒體返回媒體ID/// </summary>public static string UploadMedia(string access_token, string type, string path){    // 設置參數    string url = string.Format("http://file.api.weixin.QQ.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type);    return HttpRequestUtil.HttpUploadFile(url, path);}
View Code
string msg = WXApi.UploadMedia(access_token, "image", path); // 上圖片返回媒體IDstring media_id = Tools.GetJsonValue(msg, "media_id");
View Code

傳入的path(aspx.cs文件中的代碼):

string path = MapPath(data);
View Code

一個圖文消息由若干條圖文組成,每條圖文有標題、內容、鏈接、圖片等

遍歷每條圖文數據,分別請求微信接口,上傳圖片,獲取media_id

3、上傳圖文消息素材

拼接圖文消息素材Json字符串(ImgItemDal類(操作圖文消息表的類)):

/// <summary>/// 拼接圖文消息素材Json字符串/// </summary>public static string GetArticlesJsonStr(PageBase page, string access_token, DataTable dt){    StringBuilder sbArticlesJson = new StringBuilder();    sbArticlesJson.Append("{/"articles/":[");    int i = 0;    foreach (DataRow dr in dt.Rows)    {        string path = page.MapPath(dr["ImgUrl"].ToString());        if (!File.Exists(path))        {            return "{/"code/":0,/"msg/":/"要發送的圖片不存在/"}";        }        string msg = WXApi.UploadMedia(access_token, "image", path); // 上圖片返回媒體ID        string media_id = Tools.GetJsonValue(msg, "media_id");        sbArticlesJson.Append("{");        sbArticlesJson.Append("/"thumb_media_id/":/"" + media_id + "/",");        sbArticlesJson.Append("/"author/":/"" + dr["Author"].ToString() + "/",");        sbArticlesJson.Append("/"title/":/"" + dr["Title"].ToString() + "/",");        sbArticlesJson.Append("/"content_source_url/":/"" + dr["TextUrl"].ToString() + "/",");        sbArticlesJson.Append("/"content/":/"" + dr["Content"].ToString() + "/",");        sbArticlesJson.Append("/"digest/":/"" + dr["Content"].ToString() + "/",");        if (i == dt.Rows.Count - 1)        {            sbArticlesJson.Append("/"show_cover_pic/":/"1/"}");        }        else        {            sbArticlesJson.Append("/"show_cover_pic/":/"1/"},");        }        i++;    }    sbArticlesJson.Append("]}");    return sbArticlesJson.ToString();}
View Code

上傳圖文消息素材,獲取圖文消息的media_id:

/// <summary>/// 請求Url,發送數據/// </summary>public static string PostUrl(string url, string postData){    byte[] data = Encoding.UTF8.GetBytes(postData);    // 設置參數    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;    CookieContainer cookieContainer = new CookieContainer();    request.CookieContainer = cookieContainer;    request.AllowAutoRedirect = true;    request.Method = "POST";    request.ContentType = "application/x-www-form-urlencoded";    request.ContentLength = data.Length;    Stream outstream = re
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 札达县| 大足县| 区。| 永定县| 滦平县| 潢川县| 阳江市| 普洱| 和龙市| 久治县| 天水市| 岢岚县| 应用必备| 乐至县| 冕宁县| 商河县| 乌鲁木齐县| 化德县| 宁阳县| 鹿邑县| 阳山县| 乐东| 石家庄市| 潮安县| 施甸县| 苗栗县| 和政县| 吕梁市| 泗洪县| 巴林左旗| 宁夏| 班戈县| 邵阳县| 平潭县| 临清市| 安宁市| 雷山县| 西吉县| 游戏| 裕民县| 阿拉善盟|