C#-微信公众平台接口-上传临时素材
最煩做微信公眾平臺的東西。。文檔說得不清不楚,又沒示例代碼,只能自己 慢慢搜索,弄了一晚上,基本弄出來了,把本地的圖片上傳到微信的臨時(shí)素材那里,返回媒體ID,用于其他操作,代碼如下 :(自己導(dǎo)入相應(yīng)的類System.Net.Http,JSON解析用的LitJson)
? ? ? ? /// <summary>
? ? ? ? /// 上傳臨時(shí)素材
? ? ? ? /// 返回media_id
? ? ? ? /// </summary>
? ? ? ? /// <param name="userid"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public string UploadLinShiSuCai(int userid) {
? ? ? ? ? ? string imgpath = HttpContext.Current.Server.MapPath($"/upload/erweima/{userid}_2.png");
? ? ? ? ? ? string appid = WxPayConfig.APPID;
? ? ? ? ? ? string secret = WxPayConfig.APPSECRET;
? ? ? ? ? ? //1. 獲取AccessToken(有效期7200秒,開發(fā)者必須在自己的服務(wù)全局緩存access_token)
? ? ? ? ? ? string url1 = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
? ? ? ? ? ? string result = HttpService.Get(url1);
? ? ? ? ? ? JsonData jd = JsonMapper.ToObject(result);
? ? ? ? ? ? string access_token = (string)jd["access_token"];
? ? ? ? ? ? string url2 = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image";
? ? ? ? ? ? //圖片轉(zhuǎn)為流
? ? ? ? ? ? Image img = new Bitmap(imgpath);
? ? ? ? ? ? MemoryStream stream = new MemoryStream();
? ? ? ? ? ? img.Save(stream, ImageFormat.Png);
? ? ? ? ? ? BinaryReader br = new BinaryReader(stream);
? ? ? ? ? ? byte[] data = stream.ToArray();
? ? ? ? ? ? stream.Close();
? ? ? ? ? ? var boundary = "fbce142e-4e8e-4bf3-826d-cc3cf506cccc";
? ? ? ? ? ? var client = new HttpClient();
? ? ? ? ? ? client.DefaultRequestHeaders.Add("User-Agent", "KnowledgeCenter");
? ? ? ? ? ? client.DefaultRequestHeaders.Remove("Expect");
? ? ? ? ? ? client.DefaultRequestHeaders.Remove("Connection");
? ? ? ? ? ? client.DefaultRequestHeaders.ExpectContinue = false;
? ? ? ? ? ? client.DefaultRequestHeaders.ConnectionClose = true;
? ? ? ? ? ? var content = new MultipartFormDataContent(boundary);
? ? ? ? ? ? content.Headers.Remove("Content-Type");
? ? ? ? ? ? content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
? ? ? ? ? ? var contentByte = new ByteArrayContent(data);
? ? ? ? ? ? content.Add(contentByte);
? ? ? ? ? ? contentByte.Headers.Remove("Content-Disposition");
? ? ? ? ? ? contentByte.Headers.TryAddWithoutValidation("Content-Disposition", $"form-data; name=\"media\";filename=\"{userid}_2.png\"" + "");
? ? ? ? ? ? contentByte.Headers.Remove("Content-Type");
? ? ? ? ? ? contentByte.Headers.TryAddWithoutValidation("Content-Type", "image/png");
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var result2 = client.PostAsync(url2, content);
? ? ? ? ? ? ? ? if (result2.Result.StatusCode != HttpStatusCode.OK)
? ? ? ? ? ? ? ? ? ? throw new Exception(result2.Result.Content.ReadAsStringAsync().Result);
? ? ? ? ? ? ? ? string jsonstr = result2.Result.Content.ReadAsStringAsync().Result;
? ? ? ? ? ? ? ? JsonData jd2 = JsonMapper.ToObject(jsonstr);
? ? ? ? ? ? ? ? result = (string)jd2["media_id"];
? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw new Exception(ex.Message + ex.InnerException.Message);
? ? ? ? ? ? }?
? ? ? ? }
總結(jié)
以上是生活随笔為你收集整理的C#-微信公众平台接口-上传临时素材的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android RuntimePermi
- 下一篇: html中空格字符实体整理