发送图片类型的消息,必须先上传文件,然后才能发送图片
1上传图片
///
/// 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
///
///
///
///
///
public static PostMediaResult Create(string filePath, string type, int AgentId = 0)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}";
var url = string.Format(urlFormat, BLLAccessToken.GetAccessToken(AgentId), type);
WebUtils wut = new WebUtils();
FileItem item = new FileItem(filePath);
string sendResult = wut.DoPostFile(url, item);
//数据不用加密发送
LogInfo.Info("发送提交文件消息: " + "url=" + url);
PostMediaResult tempAccessTokenjson = Tools.JsonStringToObj(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送提交文件返回错误: " + sendResult + "url=" + url);
// return false;
}
return tempAccessTokenjson;
}
2发送图片消息
public static bool SendPicMessage(string filePath, string ucode, int AgentId, string toparty = null)
{
if (string.IsNullOrEmpty(filePath))
{
LogInfo.Error("发送提交文件消息" + "filePath为空忽略:");
return false;
}
if (string.IsNullOrEmpty(ucode) && string.IsNullOrEmpty(toparty))
{
LogInfo.Error("发送提交文件消息tag,user,party全是空,消息为:" + filePath);
return false;
}
PostMediaResult tr = Create(filePath, "image", AgentId);
if (tr.HasError())
{
return false;
}
ImageMsg textmsg = new ImageMsg(tr.media_id);
if (!string.IsNullOrEmpty(ucode))
{
textmsg.touser = ucode;
}
if (!string.IsNullOrEmpty(toparty))
{
textmsg.toparty = toparty;
}
textmsg.agentid = AgentId;
return ConmonWeixin.Msg.BLLMsg.SendMessage(textmsg);
}
3相关类
public class PostMediaResult : ReturnResult
{
///
/// 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
///
public string type { get; set; }
///
/// 媒体文件上传后获取的唯一标识,3天内有效 "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
///
public string media_id { get; set; }
///
/// 媒体文件上传时间戳 "1380000000"
///
public string created_at { get; set; }
}
public class ImageMsg : MsgBase
{
public ImageMsg(string media_id)
{
this.image = new ImageMsgContent(media_id);
this.msgtype = MsgType.GetMsgTypeText(MsgType.MsgBaseEnum.image);
}
public ImageMsgContent image { get; set; }
}
public class ImageMsgContent
{
public ImageMsgContent(string media_id)
{
this.media_id = media_id;
}
public string media_id { get; set; }
}
///
/// 执行带文件上传的HTTP POST请求。
///
/// 请求地址
/// 请求文件参数
/// HTTP响应
public string DoPostFile(string url, FileItem fileParams)
{
try
{
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
string startboundary = "--" + boundary;
string endboundary = "--" + boundary + "--";
HttpWebRequest req = GetWebRequest(url, "POST");
req.ContentType = "multipart/form-data;boundary=" + boundary;
System.IO.Stream reqStream = req.GetRequestStream();
//开始结束的换行符不能少,否则是44001,"errmsg":"empty media data,
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n" + endboundary + "\r\n");
string name = fileParams.GetFileName();
string filename = fileParams.GetFileName();
//结束的两个换行符不能少,否则是44001,"errmsg":"empty media data,
string fileTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"; filelength={2}\r\nContent-Type: {3}\r\n\r\n";
FileItem fileItem = fileParams;
byte[] fileBytes = fileItem.GetContent();
StringBuilder sb = new StringBuilder();
sb.Append(startboundary);
sb.Append("\r\n");
sb.Append(string.Format(fileTemplate, name, filename, fileBytes.Length, fileItem.GetMimeType()));
// LogInfo.Error("sb.ToString()=" + sb.ToString());
byte[] Content = Encoding.UTF8.GetBytes(sb.ToString());
//开始标志
reqStream.Write(Content, 0, Content.Length);
//文件内容
reqStream.Write(fileBytes, 0, fileBytes.Length);
//结束标志
reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
// LogInfo.Error("endBoundaryBytes=" + endboundary);
reqStream.Close();
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}
catch (WebException ex)
{
LogInfo.Error("调用微信接口异常WebException,this._timeout" + this._timeout + ",url=" + url, ex);
ReturnResult rt = new ReturnResult();
rt.errcode = 41001;
rt.errmsg = "调用微信接口异常WebException;" + ex.Message;
return Tools.ToJsonString(rt);
}
}
其他类型参考