背景:
有两个微信企业号,一个是生产的微信企业号(未升级成企业微信),一个是测试企业号(已经升级到企业微信)
在调用生产环境下的微信企业号的推送消息的接口时遇到以下问题:
{"errcode":40033,"errmsg":"invalid charset. please check your request, if include \\uxxxx will create fail!"}
根据上述,我们可以得知是由于含有特殊字符 \uxxxx
但是在测试环境中的企业号却可以发送同样的字符。
同样调用一模一样的代码去推送消息,如下:
public static string SendMsgTest(string user, string msg) { try { var item = new { touser = user, toparty = "", totag = "", msgtype = "text", agentid = 0, text = new { content = msg }, safe = "0" }; var body = DataToJson(item); WX_Api api = new WX_Api(false); string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + GetWeiQyToKenTest(); return api.InfoPost(url, body); } catch (Exception e) { return e.Message; } }
好吧,那就找推送日志:
{"touser":"C02","toparty":"","totag":"","msgtype":"text","agentid":0,"text":{"content":"测试消息,来自Perwen的补货订单审核确认:[单据号:RP1706301118_50] http://xxxx.com.cn/w/ReplenishmentOrder/Checking.aspx?code=RP1706301118_50u0026id=106u0026t=999u0026a=2222u0026v=ooooo"},"safe":"0"} {"touser":"C02","toparty":"","totag":"","msgtype":"text","agentid":0,"text":{"content":"测试消息,来自Perwen的补货订单审核确认:[单据号:RP1706301118_50] http://xxxx.com.cn/w/ReplenishmentOrder/Checking.aspx?code=RP1706301118_50u0026id=106u0026t=999u0026a=2222u0026v=ooooo"},"safe":"0"}
于是就是懵了。。。。
好吧,既然明面上不知道什么原因。但是有40033错误提示,那么就排除收干扰的因素。在生产的企业号替换掉含有特殊字符 \uxxxx 的。然后就可以了。。。。。。。。。。。
public static string SendMsg(string user,string msg) { try { var item = new { touser = user, toparty = "", totag = "", msgtype = "text", agentid = Agentid, text = new { content = msg }, safe = "0" }; var body = DataToJson(item); string Strbody = body; JavaScriptSerializer js = new JavaScriptSerializer(); body = js.Serialize(Strbody).Replace("\u0026", "&"); body = body.Replace("\", ""); body = body.TrimStart('"').TrimEnd('"'); WXApi api = new WXApi(false); string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Access_Token.Access_Token; WriteLog("url---" + url); WriteLog(" body---" + body); return api.InfoPost(url, body); } catch (Exception e) { return e.Message; } }
结论:个人猜测是企业号跟企业微信的机制有一点点不一样。(PS:园友如有真相,还望告知)