C # – WeChat public platform interface – upload temporary material

Return: http://blog.niunan.net/blog/show/1212

The most annoying thing to do on WeChat public platform. . The document is unclear, and there is no sample code. I can only search slowly by myself. After working all night, I basically figured it out. Upload the local picture to the temporary material of WeChat and return the media ID for other operations. , The code is as follows: (Import the corresponding class System.Net.Http, LitJson for JSON parsing)

 / // 

/// Upload temporary materials
/// Back to media_id
///

///
///
public string UploadLinShiSuCai(int userid) {
string imgpath = HttpContext.Current.Server.MapPath($"< /span>/upload/erweima/{userid}_2.png");
string appid = WxPayConfig.APPID;
string secret = WxPayConfig.APPSECRET;

//1. Get AccessToken (validity period is 7200 seconds, developer must Cache access_token globally in your own service)
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"< span style="color: #000000;">];

string url2 = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image";

//Picture to stream
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 span>");
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);
}

}
ASP.NET

 /// 

/// Upload temporary materials
/// Back to media_id
///

///
///
public string UploadLinShiSuCai(int userid) {
string imgpath = HttpContext.Current.Server.MapPath($"< /span>/upload/erweima/{userid}_2.png");
string appid = WxPayConfig.APPID;
string secret = WxPayConfig.APPSECRET;

//1. Get AccessToken (validity period is 7200 seconds, developer must Cache access_token globally in your own service)
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"< span style="color: #000000;">];

string url2 = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image";

//Picture to stream
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 span>");
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);
}

}
ASP.NET

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3063 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.