c# URL POST Web API



我正在尝试做一个与WECHAT API一起工作的函数,这是我的代码:

我使用下面的代码来获得连接令牌

internal static string Token(string CorpID, string Secret)
{
CorpID = CorpID ?? "wwe1f80304633";
Secret = Secret ?? "Ev7_oVN7RqD9k4yUy5pzkfcZ_QhX9l0VjZnAQ";
string token;
using (var wc = new WebClient())
{
token = wc.DownloadString($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}");
}
if (token.Contains("access_token"))
{
return token.Split(',')[2].Split(':')[1].Replace(""", "");
}
return "";
}

从WECHAT服务器获得有效令牌是成功的

下面的代码是我想向WECHAT API发出请求,并要求WECHAT向所选部门人员发送消息。

internal static string SendMsg(string sendtext)
{
string ACTOKEN = "" + PDC.MSGTOKEN + "";
string CONTENT = "" + PDC.CONTENT + "";
string PostUrl;
using (var wc2 = new WebClient())
{
PostUrl = wc2.UploadString($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}{1}", ACTOKEN, CONTENT);
}

return "";
}
public static void SendMsg2()
{
PDC.CONTENT = "Test Message";
string MsgContent = "{"toparty": "" + PDC.DEPTID + "","msgtype": "text","agentid": "" + PDC.AGENTID + "","text": {"content": "" + PDC.CONTENT + ""},"safe":0}";
SendMsg(MsgContent);
MessageBox.Show("" + MsgContent + "");
}

我在我的WinForm上添加了一个按钮,并试图让它在上工作

private void BtnSendMsg_Click(object sender, EventArgs e)
{
string token = MSG.Token(null, null);
if (!string.IsNullOrEmpty(token))
{
PDC.MSGTOKEN = token;
MessageBox.Show("" + PDC.MSGTOKEN + "");
}
else
{
MessageBox.Show(" Invalid Token ");
}
MSG.SendMsg2();
}

然而,它似乎不起作用,如果我没有错的话,这部分的问题

internal static string SendMsg(string sendtext)
{
string ACTOKEN = "" + PDC.MSGTOKEN + "";
string CONTENT = "" + PDC.CONTENT + "";
string PostUrl;
using (var wc2 = new WebClient())
{
PostUrl = wc2.UploadString($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}{1}", ACTOKEN, CONTENT);
}

return "";
}

有人能给我一些如何解决这个问题的想法吗?很多很多谢谢~

我已经完成了我的代码,没有问题,这是下面的代码,适合所有需要的人。

从Https API 获取令牌的代码

internal static string Token(string CorpID, string Secret)
{
CorpID = CorpID ?? "" + PDC.CorpID + "";
Secret = Secret ?? "" + PDC.Secret + "";
string token;
using (var wc = new WebClient())
{
token = wc.DownloadString($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}");
}
if (token.Contains("access_token"))
{
return token.Split(',')[2].Split(':')[1].Replace(""", "");
}
return "";
}

POST 方法

internal static string PostWebRequest(string PostUrl, string ParamData, Encoding DataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = DataEncode.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)
{
return ex.Message;
}
return ret;
}

使用WECHAT WORK发送消息的代码

internal static string SendMsg(string CorpID, string Secret, string ParamData, Encoding DataEncode)
{
string AccessToken = Token(CorpID, Secret);
string PostUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", AccessToken);
return PostWebRequest(PostUrl, ParamData, DataEncode);
}
public static void SendMsg2()
{
string sCorpID = "" + PDC.CorpID + "";
string sSecret = "" + PDC.Secret + "";
PDC.CONTENT = "Test Message";
string Message = "Test";

string MsgContent = "{";
MsgContent += ""totag": "" + PDC.DEPTID + "",";
MsgContent += ""msgtype": "text",";
MsgContent += ""agentid": "" + PDC.AGENTID + "",";
MsgContent += ""text": {";
MsgContent += "  "content": "" + Message + """;
MsgContent += "},";
MsgContent += ""safe":"0"";
MsgContent += "}";
SendMsg(sCorpID, sSecret, MsgContent, Encoding.UTF8);
}

激活发送消息功能的按钮事件

private void BtnSendMsg_Click(object sender, EventArgs e)
{
string token = MSG.Token(null, null);
if (!string.IsNullOrEmpty(token))
{
PDC.MSGTOKEN = token;
}
MSG.SendMsg2();
}

最新更新