protected void btnUpload_Click(object sender, EventArgs e)
{
#region
if (PublicParams.tokenWeiChart != null)
{
//isFile 上传图片的按钮Id
if (isFile.HasFile)
{
string savedPath = Server.MapPath(“~/uploads/”); //保存文件的路径
string fileName = savedPath + isFile.FileName;
isFile.SaveAs(fileName);
FileStream fs1 = new FileStream(isFile.FileName, FileMode.Open);
StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding(“GBK”));
string dataImg = sr.ReadToEnd();
sr.Close();
fs1.Close();
Stream outstream = null;
Stream instream = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(dataImg);
#region
string str = “{“media”:[{“filename”:”” + isFile.FileName + “”,”filelength”:”” + data.Length + “”,”content-type”:”text/plain”}]}”;
string url = “http://api.weixin.qq.com/cgi-bin/material/add_news?access_token=” + PublicParams.tokenWeiChart + “&type=image&media=” + str;
#endregion
// 准备请求…
try
{
// 设置参数
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;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd(); //返回结果
}
catch (Exception ex)
{
string err = ex.Message;
}
}
}
else
{
CreateToken.CreateNewToken(); //Token不存在时,调用生成方法
}
#endregion
}
但是它一直返回 44003 即 图文消息内容为空
求指导
40
是不是你在上传图片到微信服务器时,media参数有问题。