c# URL POST 网络 API

c# URL POST Web API

我正在尝试做一个与微信一起工作的功能API, 这是我的代码:

我使用下面的代码获取一个连接Token

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 "";
    }

微信服务器获取有效token成功,

下面的代码是我想POST向微信API发送一个请求,让微信发送消息给选定的部门人员。

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获取Token的代码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();
    }