#设置接收事件服务器
#openapi回调地址请求不通过?
#企业微信api
#添加外部联系人事件
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data;
using System.Xml.Serialization;
using System.Xml;
using System.Net.Http;
namespace WebApi2
{
public class TestController : ApiController
{
//公司
string sCorpID = "*********************";
string sToken = "***************************";
string sEncodingAESKey = "*************************";
public HttpResponseMessage Get()
{
HttpContext context = System.Web.HttpContext.Current;
context.Response.ContentType = "text/plain; charset=utf-8"; //
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//context.Response.Headers.Add("Content-type", "text/html; charset=utf-8");///html
context.Response.StatusCode = 200;
using (var reader = new System.IO.StreamReader(context.Request.InputStream))
{
String xmlData = reader.ReadToEnd();
//string Url = System.Web.HttpUtility.UrlDecode(context.Request.Url.ToString());
string Url = context.Request.Url.ToString();
if (Url != "") Url = Url.Split('?')[1];
string param = string.Empty;
if (!string.IsNullOrEmpty(Url))
{
string msg_signature = "";
string timestamp = "";
string nonce = "";
string echostr = xmlData; //密文
List<string> para = new List<string>(Url.Split('&'));
foreach (var item in para)
{
if (item.Contains("="))
{
string[] dic = item.Split('=');
if (dic[0] == "msg_signature")
{
msg_signature = dic[1];
}
if (dic[0] == "timestamp")
{
timestamp = dic[1];
}
if (dic[0] == "nonce")
{
nonce = dic[1];
}
if (dic[0] == "echostr")
{
echostr = dic[1];
}
}
}
if (para.Count == 0)
{
//return;
}
Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID);
string sVerifyMsgSig = msg_signature;
string sVerifyTimeStamp = timestamp;
string sVerifyNonce = nonce;
string sVerifyEchoStr = echostr + "==";//
int ret = 0;
string sEchoStr = "";
//wxcpt.VerifyURL 可以在企业微信api官网上下载到已经封装好的
// https://developer.work.weixin.qq.com/devtool/introduce?id=10128
ret = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr, ref sEchoStr);
if (ret != 0)
{
System.Console.WriteLine("ERR: VerifyURL fail, ret: " + ret);
HttpResponseMessage responseMessage = new HttpResponseMessage { Content = new StringContent("校验失败!", Encoding.GetEncoding("UTF-8"), "text/plain") };
return responseMessage;
}
else
{
//写入数据库
if (执行事务操作(sEchoStr))
{
}
else
{
}
//去掉回传字符串的 双引号
HttpResponseMessage responseMessage = new HttpResponseMessage { Content = new StringContent(sEchoStr, Encoding.GetEncoding("UTF-8"), "text/plain") };
return responseMessage;
}
//
/*
POST
正确响应本次请求
· 企业微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次
· 当接收成功后,http头部返回200表示接收ok,其他错误码企业微信后台会一律当做失败并发起重试
回复
*
* 步骤4中,不同的业务回调要求返回不同内容。比如回复空串,或者特定字符串(如success),
* 以及上一步构造的加密被动回复消息。具体要求在各个回调业务文档会有说明。
GET
在1秒内响应GET请求,响应内容为上一步得到的明文消息内容(不能加引号,不能带bom头,不能带换行符)
*/
}
}
HttpResponseMessage Result = new HttpResponseMessage { Content = new StringContent("要返回的明文", Encoding.GetEncoding("UTF-8"), "text/plain") };
return Result;
}
企业微信官方
(2018年10月11日更新,点击下载)
注意事项:
Cryptography.cs文件封装了AES加解密过程,用户无须关心具体实现。WXBizMsgCrypt.cs文件提供了用户接入企业微信的三个接口,Sample.cs文件提供了如何使用这三个接口的示例。
WXBizMsgCrypt.cs封装了VerifyURL, DecryptMsg, EncryptMsg三个接口,分别用于开发者验证接收消息的url、接收消息的解密以及开发者回复消息的加密过程。使用方法可以参考Sample.cs文件。
下载地址
https://developer.work.weixin.qq.com/devtool/introduce?id=10128