public class QYWeixinHelper
{
private static int[] pyValue = new int[]
{
-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,
-20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,
-19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,
-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,
-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,
-19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,
-18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,
-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,
-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,
-17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,
-17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,
-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,
-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,
-16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,
-15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,
-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,
-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,
-15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,
-14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902,
-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654,
-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,
-14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,
-14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907,
-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,
-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,
-13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,
-13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831,
-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300,
-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,
-11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,
-11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832,
-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328,
-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254
};
private static string[] pyName = new string[]
{
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can",
"Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng",
"Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong",
"Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De",
"Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui",
"Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo",
"Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong",
"Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han",
"Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan",
"Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing",
"Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke",
"Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo",
"La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang",
"Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun",
"Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian",
"Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang",
"Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning",
"Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan",
"Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po",
"Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu",
"Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou",
"Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen",
"Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu",
"Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan",
"Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian",
"Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai",
"Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao",
"Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang",
"Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun",
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",
"Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan",
"Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"
};
public static string corpid = ""; //企业微信的企业ID
public static string appsecret = "";//企业微信里面的建立的小程序的Secret
public static string messageSendURI = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
public static string chaturl = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={0}";//发送消息到群
public static string getAccessTokenUrl = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={appsecret}";
public static int agent_id = ;//企业微信里面的建立的小程序的APPID
//获取token
public static string GetQYAccessToken()
{
string accessToken = "";
string respText = "";
//获取josn数据
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getAccessTokenUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream resStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(resStream, Encoding.Default);
respText = reader.ReadToEnd();
resStream.Close();
}
try
{
if (respText.Length > 0)
{
JObject jObject = JObject.Parse(respText);
accessToken = jObject.SelectToken("access_token").ToString();
}
}
catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); }
return accessToken;
}
//发送web请求方法
public static string PostWebRequest(string postUrl, string paramData)
{
string ret = string.Empty;
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return "";
}
return ret;
}
//发送文本消息
/// <summary>
///
/// </summary>
/// <param name="name">userid</param>
/// <param name="message">文本信息</param>
public static void Send_Message_byname(string name, string message)
{
List<string> list = new List<string>();
if (name.Contains("|"))
{
list = name.Split('|').ToList();
for (int j = 0; j < list.Count; j++)
{
string name_dan = list[j];
if (IsChinese(name_dan))
{
string touser = "";
string xing = name_dan.Substring(0, 1);
string ming = name_dan.Substring(1, name_dan.Length - 1);
touser = Convert_Pinyin(ming) + "." + Convert_Pinyin(xing);
//发送文本消息
string res = SendText(touser, message);
if (res.Length > 0)
{
JObject jo = JObject.Parse(res);
string errmsg = jo.SelectToken("errmsg").ToString();
if (errmsg != "ok")
{
MessageBox.Show(errmsg);
}
}
}
else
{
MessageBox.Show($"名字:{name}不全是中文!");
return;
}
}
}
else
{
if (IsChinese(name))
{
string touser = "";
string xing = name.Substring(0, 1);
string ming = name.Substring(1, name.Length - 1);
touser = Convert_Pinyin(ming) + "." + Convert_Pinyin(xing);
//发送文本消息
string res = SendText(touser, message);
if (res.Length > 0)
{
JObject jo = JObject.Parse(res);
string errmsg = jo.SelectToken("errmsg").ToString();
if (errmsg != "ok")
{
MessageBox.Show(errmsg);
}
}
}
else
{
MessageBox.Show($"名字:{name}不全是中文!");
return;
}
}
}
public static string SendUserInfo(int sendtype, string message)
{
string res = "";
string accessToken = GetQYAccessToken();
string postUrl = "";
if (sendtype == 0)
{
postUrl = string.Format(messageSendURI, accessToken);
}
else
{
postUrl = string.Format(chaturl, accessToken);
}
res = PostWebRequest(postUrl, message);
return res;
}
//发送图片消息
public static void Send_Pic_byname(string name, string pic_path)
{
string media_id = Upload_Pic(pic_path);
if (media_id.Length <= 0) { return; }
if (IsChinese(name))
{
string touser = "";
for (int i = 0; i < name.Length; i++)
{
string xing = name.Substring(0, 1);
string ming = name.Substring(1, name.Length - 1);
touser = Convert_Pinyin(ming) + "." + Convert_Pinyin(xing);
//string user = Useridjiami(touser);
}
//发送图片消息
string res = SendPicture(touser, media_id);
if (res.Length > 0)
{
JObject jo = JObject.Parse(res);
string errmsg = jo.SelectToken("errmsg").ToString();
if (errmsg != "ok")
{
MessageBox.Show(errmsg);
}
}
}
else
{
MessageBox.Show($"名字:{name}不全是中文!");
return;
}
}
//用户加密
public static string Useridjiami(string userid) {
string res = "";
string accessToken = GetQYAccessToken();
string postUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/batch/userid_to_openuserid?access_token={0}", accessToken);
var json_req = new
{
userid_list = userid
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将json转字符串
res = PostWebRequest(postUrl, jsonRequest);
return res;
}
//发送文本消息
public static string SendText(string empName, string message)
{
string res = "";
string accessToken = GetQYAccessToken();
string postUrl = string.Format(messageSendURI, accessToken);
CorpSendText paramData = new CorpSendText(message);
foreach (string item in empName.Split('|'))
{
paramData.touser = item;
string param = JsonConvert.SerializeObject(paramData);
if (paramData.touser != null)
{
res = PostWebRequest(postUrl, param);
}
else
{
res = "账号" + paramData.touser + "不存在!";
}
}
return res;
}
//发送图片消息
public static string SendPicture(string empName, string media_id)
{
string res = "";
string accessToken = GetQYAccessToken();
string postUrl = string.Format(messageSendURI, accessToken);
CorpSendPicture paramData = new CorpSendPicture(media_id);
foreach (string item in empName.Split('|'))
{
paramData.touser = item;
string param = JsonConvert.SerializeObject(paramData);
if (paramData.touser != null)
{
res = PostWebRequest(postUrl, param);
}
else
{
res = "账号" + paramData.touser + "不存在!";
}
}
return res;
}
//上传图片后获取media_id
public static string Upload_Pic(string path)
{
string media_id = "";
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}";
var url = string.Format(urlFormat, GetQYAccessToken(), "image");
string sendResult = DoPostFile(url, path);
if (sendResult.Length > 0)
{
JObject jo = JObject.Parse(sendResult);
media_id = jo.SelectToken("media_id").ToString();
}
return media_id;
}
//获取图片的字节
private static byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open);
byte[] imgByte = new byte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
return imgByte;
}
//上传图片
public static string DoPostFile(string url, string path)
{
try
{
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
string startboundary = "--" + boundary;
string endboundary = "--" + boundary + "--";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(url));
req.Method = "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 = "11";
string filename = "11";
//结束的两个换行符不能少,否则是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";
byte[] fileBytes = getImageByte(path);
StringBuilder sb = new StringBuilder();
sb.Append(startboundary);
sb.Append("\r\n");
sb.Append(string.Format(fileTemplate, name, filename, fileBytes.Length, MimeMapping.GetMimeMapping(path)));
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);
reqStream.Close();
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(rsp.GetResponseStream(), Encoding.Default);
string ret = sr.ReadToEnd();
sr.Close();
return ret;
}
catch (WebException ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return "";
}
}
public class Text
{
public string content { get; set; }
}
public class Image
{
public string media_id { get; set; }
}
//发送文本消息的类
public class CorpSendText : CorpSendBase
{
private Text _text;
/// <summary>
/// 要发送的文本,必须小写,企业微信API不识别大写。
/// </summary>
public Text text
{
get { return _text; }
set { this._text = value; }
}
public CorpSendText(string content)
{
base.msgtype = "text";
this.text = new Text
{
content = content
};
}
}
//发送图片消息的类
public class CorpSendPicture : CorpSendBase
{
private Image _image;
/// <summary>
/// 要发送的文本,必须小写,企业微信API不识别大写。
/// </summary>
public Image image
{
get { return _image; }
set { this._image = value; }
}
public CorpSendPicture(string media_id)
{
base.msgtype = "image";
this.image = new Image
{
media_id = media_id
};
}
}
/// <summary>
/// 企业微信发送消息的基础消息内容
/// </summary>
public class CorpSendBase
{
/// <summary>
/// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
/// </summary>
public string touser { get; set; }
/// <summary>
/// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string toparty { get; set; }
/// <summary>
/// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string totag { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string msgtype { get; set; }
/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
public int agentid { get; set; }
/// <summary>
/// 表示是否是保密消息,0表示否,1表示是,默认0
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string safe { get; set; }
public CorpSendBase()
{
this.agentid = QYWeixinHelper.agent_id;
this.safe = "0";
}
}
//判断名字是否全是汉字
public static bool IsChinese(string str_chinese)
{
bool b = true;
for (int i = 0; i < str_chinese.Length; i++)
{
Regex reg = new Regex(@"[\u4e00-\u9fa5]");
if (!reg.IsMatch(str_chinese[i].ToString()))
{
b = false;
break;
}
}
return b;
}
//汉字名字转成拼音
public static string Convert_Pinyin(string hzString)
{
// 匹配中文字符
Regex regex = new Regex("^[\u4e00-\u9fa5]$");
byte[] array = new byte[2];
string pyString = "";
int chrAsc = 0;
int i1 = 0;
int i2 = 0;
char[] noWChar = hzString.ToCharArray();
for (int j = 0; j < noWChar.Length; j++)
{
// 中文字符
if (regex.IsMatch(noWChar[j].ToString()))
{
array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
i1 = (short)(array[0]);
i2 = (short)(array[1]);
chrAsc = i1 * 256 + i2 - 65536;
if (chrAsc > 0 && chrAsc < 160)
{
pyString += noWChar[j];
}
else
{
// 修正部分文字
if (chrAsc == -9254) // 修正“圳”字
pyString += "Zhen";
else
{
for (int i = (pyValue.Length - 1); i >= 0; i--)
{
if (pyValue[i] <= chrAsc)
{
pyString += pyName[i];
break;
}
}
}
}
}
// 非中文字符
else
{
pyString += noWChar[j].ToString();
}
}
return pyString;
}
/// <summary>
/// 企业微信群发送图片
/// </summary>
/// <param name="chatid"></param>
/// <param name="path"></param>
/// <returns></returns>
public QYSendResult QYWXSendChart2(string chatid, string path)
{
try
{
string media_id = QYWeixinHelper.Upload_Pic(path);
string url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=" + GetQYAccessToken() + "";
var json_req = new
{
chatid = chatid,//获取群会话id
msgtype = "image",
image = new
{
media_id = media_id
},
safe = 0
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将json转位字符串
var response = ApiRequest.HttpPost(url, jsonRequest);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QYSendResult>(response);
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
#region begin
public class QYWeiChartHelper
{
public string qycorpid = "";//企业id
public string SECRET = "";//应用的SECRET
//获取应用token
public QYTokenResult QYWXGetToken()
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + qycorpid + "&corpsecret=" + SECRET + "";
try
{
var response = ApiRequest.HttpGet(url);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QYTokenResult>(response);
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//创建群会话-- chatid需要自定义
public QYCreateResult GetQYWXcreateChart(string accessToken)
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=" + accessToken + "";
string param = "{\"name\": \"企业内部群\",\"owner\": \"6005\",\"userlist\": [\"6005\",\"6004\"],\"chatid\": \"558\"}";
try
{
var response = ApiRequest.HttpPost(url, param);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QYCreateResult>(response);
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//发送群消息 --文本
public QYSendResult QYWXSendChart(string accessToken, string chatid, string text)
{
try
{
QYMessageResult mr = new QYMessageResult() { chatid = chatid, msgtype = "text", text = new Text { content = text }, safe = 0 };
string param = JsonConvert.SerializeObject(mr);
string url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=" + accessToken + "";
var response = ApiRequest.HttpPost(url, param);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<QYSendResult>(response);
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 通过号码获取userid
/// </summary>
/// <param name="accessToken"></param>
/// <param name="pone"></param>
/// <returns></returns>
public string PhoneGetUser(string accessToken, string pone)
{
try
{
string MessageUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token={0}", accessToken);
var json_req = new
{
mobile = pone
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将json转字符串
var response = ApiRequest.HttpPost(MessageUrl, jsonRequest);
return response;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 邮箱获取userid
/// </summary>
/// <param name="accessToken"></param>
/// <param name="email"></param>
/// <returns></returns>
public string EmailGetUser(string accessToken, string email)
{
try
{
string MessageUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/get_userid_by_email?access_token={0}", accessToken);
var json_req = new
{
email = email,
email_type = 1//邮箱类型:1-企业邮箱(默认);2-个人邮箱
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将json转字符串
var response = ApiRequest.HttpPost(MessageUrl, jsonRequest);
return response;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
public class QYTokenResult
{
public int errcode { get; set; }
public string errmsg { get; set; }
public string access_token { get; set; }
public int expires_in { get; set; }
}
public class QYCreateResult
{
public int errcode { get; set; }
public string errmsg { get; set; }
public string chatid { get; set; }
}
public class QYMessageResult
{
public string chatid { get; set; }
public string msgtype { get; set; }
public Text text { get; set; }
public int safe { get; set; }
}
public class QYSendResult
{
public int errcode { get; set; }
public string errmsg { get; set; }
public string messageId { get; set; }
}
#endregion
#region API请求方法
public class ApiRequest
{ //get请求
public static string HttpGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
//post请求
public static string HttpPost(string url, string body)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
#endregion