namespace Syshxrcsc.M
{
/// <summary>
/// 微信入口(只是接收微信请求的URL)
/// </summary>
public class clsDatabase : IHttpHandler
{
string MyAppid = wexinConfig.Appid;
wechatCallbackAPI wapi = new wechatCallbackAPI();
Publics pbl = new Publics();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
InterfaceAPI();
}
public void InterfaceAPI()
{
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
wechatCallbackAPI.Auth();
}
else
{
string postStr = "";
if (HttpContext.Current.Request.HttpMethod.ToLower() == "post")
{
//获取内容
Stream s = HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
postStr = Encoding.UTF8.GetString(b);
if (!string.IsNullOrEmpty(postStr))
{
RequestXML xmlMsg = wapi.Handle(postStr);
ResponseMsg(xmlMsg);
}
else
{
wechatCallbackAPI.Auth();
}
}
}
}
public string QueryLocation(string location, RequestXML rxml)
{
string Content = "";
int sum = 0;
IList<CnJob> icj = CompanyJobDAL.QueryCnJob(location);
if (icj.Count > 0)
{
Content = "根据您提供的地理位置,在该位置周边共找到" + icj.Count + "条职位!\r\n" +
"职位详情如下:\r\n";
foreach (var ilist in icj)
{
Content += "" + sum + "、<a href='http://m.zptong.com/m/" + ilist.JobId + ".shtml'>" + ilist.JobName + "</a> " +
"" + ilist.MonthPay + "\r\n" +
"" + ilist.CompanyName + "\r\n";
sum += 1;
if (sum > 6)
{
Content += "<a href='http://m.zptong.com/index.html?key=" + rxml.Content + "'>点击查看更多>></a>";
break;
}
}
}
return Content;
}
public string bind()
{
// string MyAppid = "wx5fe1d808d6a01206";
string RedirectUri = "http://m.zptong.com/WxInfo.aspx"; //授权页面是由微信自动给出的,点击确定后才跳到绑定页面
string URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + MyAppid+ "&redirect_uri=" + RedirectUri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
string strContent = "你还未绑定招聘通账号[抠鼻]....\r\n请点击下面的链接开始绑定:\r\n<a href='" + URL + "'>绑定账号</a>";
return strContent;
}
/// <summary>
/// 返回文本类型的xml
/// </summary>
/// <param name="requestXML">微信请求类</param>
/// <param name="Message">要发送的文本消息</param>
/// <returns></returns>
public string RetText(RequestXML requestXML, string Message)
{
string resxml = "<xml>" +
"<ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName>" +
"<FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName>" +
"<CreateTime>" + int.Parse(DateTime.Now.ToString("yyyyMMdd")) + "</CreateTime>" +
"<MsgType><![CDATA[text]]></MsgType>" +
"<Content><![CDATA[" + Message + "]]></Content>" +
"</xml>";
return resxml;
}
/// <summary>
/// 转接到客服(没回复)
/// </summary>
/// <param name="requestXML">微信请求类</param>
/// <param name="Message">要发送的文本消息</param>
/// <returns></returns>
public string RetTextCustomer(RequestXML requestXML, string Message)
{
string resxml = "<xml>" +
"<ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName>" +
"<FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName>" +
"<CreateTime>" + int.Parse(DateTime.Now.ToString("yyyyMMdd")) + "</CreateTime>" +
"<MsgType><![CDATA[transfer_customer_service]]></MsgType>" +
"<Content><![CDATA[" + Message + "]]></Content>" +
"</xml>";
return resxml;
}
/// <summary>
/// 多图文消息xml中item节点
/// </summary>
/// <param name="title">标题</param>
/// <param name="description">描述</param>
/// <param name="picurl">图片url</param>
/// <param name="url">链接url</param>
/// <returns></returns>
public string SendItem(string title, string description, string picurl, string url)
{
return "<item>" +
"<Title><![CDATA[" + title + "]]></Title>" +
"<Description><![CDATA[" + description + "]]></Description>" +
"<PicUrl><![CDATA[" + picurl + "]]></PicUrl>" +
"<Url><![CDATA[" + url + "]]></Url>" +
"</item>";
}
/// <summary>
/// 返回图文消息、
/// </summary>
/// <param name="requestXML">微信请求类</param>
/// <param name="string">展示的数据 格式必须是:xxxx-xxx-xxx|xxxx-xxx-xxx|xxxx-xxx-xxx</param>
/// <param name="description">描述</param>
/// <param name="picurl">图片链接</param>
/// <param name="keyImg">点击图文消息跳转的链接</param>
/// <param name="Count">文章的条数</param>
/// <returns></returns>
public string SendSubscribe(RequestXML requestXML, string strData,int Count)
{
string str = "";
string res = "";
string[] all = strData.Split('|');
for (int i = 0; i < all.Length; i++)
{
string[] sAll = all[i].Split('-');
//参数:标题、描述、图片url、图片链接url
str += SendItem(sAll[0], "", sAll[1], sAll[2]);
}
res = string.Format(@"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>{3}</ArticleCount>
<Articles>
{4}
</Articles>
</xml> ",
requestXML.FromUserName, requestXML.ToUserName, DateTime.Now,Count,str);
return res;
}
/// <summary>
///
/// </summary>
/// <param name="requestXML">微信请求类</param>
/// <param name="picurl">要显示图片的URL</param>
/// <param name="title">图文消息要显示的内容</param>
/// <param name="url">图文消息的链接URL</param>
/// <returns></returns>
public string sendTextImg(RequestXML requestXML, string picurl, string title, string url,string Descri)
{
string str = "";
str = SendItem(title, Descri, picurl, url);
string res = string.Format(@"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
{3}
</Articles>
</xml> ",
requestXML.FromUserName, requestXML.ToUserName, DateTime.Now, str);
return res;
}
namespace Zptong_DAL
{
public class wechatCallbackAPI
{
private static string Token = "imToken";
weixinMenu wx = new weixinMenu();
#region 验证微信签名
private static bool CheckSignature()
{
String signature = HttpContext.Current.Request.QueryString["signature"];
String timestamp = HttpContext.Current.Request.QueryString["timestamp"];
String nonce = HttpContext.Current.Request.QueryString["nonce"];
//将上面是哪个字段进行排序
String[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp);//字典排序
//将上面三个字符串拼接成一个字符串
String tmpStr = String.Join("", ArrTmp);
//进行sha1加密
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
//与signature对比
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
public static void Auth()
{
string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"];
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
System.Web.HttpContext.Current.Response.Write(echoStr);
System.Web.HttpContext.Current.Response.End();
}
}
}
#endregion
#region 解析XML请求参数
/// <summary>
/// 微信接口地址页面方法
/// </summary>
/// <param name="postStr">请求内容</param>
public RequestXML Handle(string postStr)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(postStr);
XmlElement rootElement = doc.DocumentElement;
XmlNode MsgType = rootElement.SelectSingleNode("MsgType");
RequestXML requestXML = new RequestXML();
requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; //接收方账号
requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; //开发者微信号
requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; //消息创建时间
requestXML.MsgType = MsgType.InnerText;
//根据不同的类型进行不同的处理
switch (requestXML.MsgType.Trim())
{
case "text": //文本消息
requestXML.Content = rootElement.SelectSingleNode("Content").InnerText;
break;
case "image": //图片
requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;
break;
case "voice": //语音
requestXML.Yuyin = rootElement.SelectSingleNode("Yuyin").InnerText;
break;
case "location": //位置
requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText;
requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText;
requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText;
requestXML.Label = rootElement.SelectSingleNode("Label").InnerText;
break;
case "link": //链接
break;
case "event": //事件推送 支持V4.5+
requestXML.Event = rootElement.SelectSingleNode("Event").InnerText;
requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; //彩带的key值
break;
}
return requestXML;
}
#endregion
public void SendServeic(string openid, string msg, string title)
{
if (openid != "")
{
string res = "";
//string appid = "wx5fe1d808d6a01206";
//string secret = "18609d7c84faba8828f0b0c6668deddf";
string appid = wexinConfig.Appid;
string secret = wexinConfig.AppSecret;
string access_Token = wx.GetAccessToken(appid, secret);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_Token;
string postData = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"" + title + "\"," +
"\"description\":\"" + msg + "\",\"url\":\"\",\"picurl\":\"\"}]}}";
res = GetPage(posturl, postData);
}
}
public void SendImgServeic(string openid, string msg, string title, string url, string picurl)
{
if (openid != "")
{
string res = "";
//string appid = "wx5fe1d808d6a01206";
//string secret = "18609d7c84faba8828f0b0c6668deddf";
string appid = wexinConfig.Appid;
string secret = wexinConfig.AppSecret;
string access_Token = wx.GetAccessToken(appid, secret);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_Token;
string postData = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"" + title + "\"," +
"\"description\":\"" + msg + "\",\"url\":\"" + url + "\",\"picurl\":\"" + picurl + "\"}]}}";
res = GetPage(posturl, postData);
//System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("\"errcode\":(?<code>.*?),");
//var mat = reg.Matches(res);
//foreach (System.Text.RegularExpressions.Match item in mat)
//{
// var state = item.Groups["code"];
// if(state.ToString()!="0"){
// }
//}
}
}
public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) 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();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
HttpContext.Current.Response.Write(err);
return string.Empty;
}
}
/// <summary>
/// 发送图文消息
/// </summary>
/// <param name="openid">openid</param>
/// <param name="perid">个人id</param>
/// <param name="Indexurl">第一张图片的url</param>
/// <param name="Indexpicurl">第一张图片的picurl</param>
/// <param name="Indextitle">第一张图片的title</param>
/// <returns></returns>
public string SendnewsServeic(string openid, string perid, string Indexurl, string Indexpicurl, string Indextitle,string TwoIndexurl,string TwoIndexpicurl,string Twotitle)
{
string res = "";
if (openid != "")
{
//推荐职位的更多、url是个人后台的推荐职位页面
string strTjjobCk = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wexinConfig.Appid + "&redirect_uri=http://m.zptong.com/m_person/perRecommend.html&response_type=code&scope=snsapi_userinfo&state=scan#wechat_redirect";
//消息最右边的箭头图片url
// string keyImg = "http://m.zptong.com/img/key.jpg";
string keyImg = "https://mmbiz.qlogo.cn/mmbiz/5c8QWPZm87SHwBDfkVw2r7QibObhGe8AqO2qgSOJvSWQj4pWI44HyXtBX3b0uKCGDJyicKOSnBUyTwh8zM9aazkw/0";
// string picurl = "https://mmbiz.qlogo.cn/mmbiz/5c8QWPZm87QV3X1iaXCUNCsVyxbxH1QGRn0taibZJeWUiamBV0E8KdpUV2U9CyMcshbnwoEEYjJjpjy5lnGoSuFpw/0?wx_fmt=jpeg";
// string strIndex = "http://m.zptong.com/m_person/myResume.html?wxperid=" + perid;
// string title = "恭喜您已绑定招聘通账号:" + username + ",密码:zptong,为您更快找到工作,请您及时完善您的简历!";
string str = SendItem(Twotitle, "", TwoIndexpicurl, TwoIndexurl) + ",";
str += SendItem(Indextitle, "", Indexpicurl, Indexurl) + ",";
IList<CnJob> icj =ResumeDAL.IQueryWishJob(perid.ToString());
if (icj != null && icj.Count > 0)
{
int sum = 0;
foreach (var ilist in icj)
{
sum += 1;
string sult = string.Format("{0}、{1}\r\n{2}", sum, ilist.JobName, ilist.CompanyName);
//链接url
string strUrl ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wexinConfig.Appid + "&redirect_uri=http://m.zptong.com/m/" + ilist.JobId + ".shtml&response_type=code&scope=snsapi_userinfo&state=scan#wechat_redirect";
//xml中的item
str += SendItem(sult, "", keyImg, strUrl) + ",";
//因为ArticleCount设置为6最上面算一条、查看更多页算一条、所以只能==4
if (sum == 3)
{
// xml中的item
str += SendItem("查看更多>>", "", keyImg, strTjjobCk) + ",";
break;
}
}
}
str = str.TrimEnd(',');
string secret = wexinConfig.AppSecret;
string appid = wexinConfig.Appid;
string access_Token = wx.GetAccessToken(appid, secret);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_Token;
string postData = SendSubscribe(openid, str);
res = GetPage(posturl, postData);
}
return res;
}
一部分代码,发送图文消息时微信公众号回复该公众号暂时无法提供服务请稍后再试,之前百度觉得微信开发有着两个页面就可以了,然后今天再看看看看又觉得他还需要一个WeixinInterface.ashx”的一般处理页来响应 时间越久百度越多就越迷茫。。。求会的朋友告诉我。。。最好是可以亲自帮我看看的。。。。
首先排查服务器500错误,检查日志或异常,(可能性大)
如果不是500错误,检查微信接口协议,注意格式以及长度限制等
他返回的errorcode是多少,我记得,好像是你这个用户很久没有跟这个公众号互动了
我不晓得。。。前程序员开发的行,然后我改了其他的功能不是微信的上传Bil文件发现获取不到图文消息了 我还不晓得怎么调试。。。从没接触过这方面的现在也没人教没人问,自生自灭中。。。
@柒柒。: 你在报该公众号暂时无法提供服务的用户,发送一条消息给公众号
@a85295853: 然后呢?
@柒柒。: 然后再试下
@a85295853: 所有使用公众号的一发消息给公众号都会收到该公众号暂时无法提供服务的消息
@柒柒。: 原来是你发消息给公众号,公众号提示公众号暂时无法提供服务啊,你消息接口那里出代码错误了,你添加一个Global.asax,全局捕获异常到本地记事本里看下
@柒柒。: 先问下,你开发微信接口多久,
@a85295853: 是小白。。。而且是刚进社会的。。。这个微信接口是前程序员写的
@柒柒。: 那就比较麻烦了,你那边只有你一个开发吗,有没有技术强的人
@a85295853: http://blog.sina.com.cn/s/blog_747a45a10102v86d.html
@a85295853: 只有我一个 微信这个我花了好多时间了,周围的同学也没搞过在技术群里问少有人搭理,百度越百越懵。。。
@柒柒。: 其实都是靠自己的,http://blog.sina.com.cn/s/blog_747a45a10102v86d.html
@a85295853: 猪蠢该杀,人蠢没法。。。
@a85295853: 消息没经过服务器响应
@柒柒。: 你先百度找出现这个问题的原因,然后一个一个排除,找到答案,
@柒柒。: 那就是消息服务器都停掉了啊
@a85295853: 排除后的理解就是消息没经过服务器响应不能辨别他是啥东东
@柒柒。: 你QQ多少
@a85295853: 1612173826
@a85295853:这是要帮我远程的节奏咩
@柒柒。: 老是在这里回复太累