简体   繁体   English

c#URL POST Web API

[英]c# URL POST Web API

I'm trying to do a function which work with WECHAT API, Here is my code: 我正在尝试使用WECHAT API的功能,这是我的代码:

I use the code below to get a connection 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 "";
    }

It's successful to get a valid token from WECHAT Server, 从WECHAT Server获得有效令牌是成功的,

And the code below is I want to POST a request to WECHAT API and ask WECHAT to Send Message to selected Department Person. 下面的代码是我想向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 + "");
    }

And I Added a button on my WinForm and trying to make it work 我在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();
    }

However seems it doesn't work, and if I'm not wrong the problem on this part 但是,这似乎不起作用,如果我没记错的话,这部分的问题

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

May anyone can give me some ideas how I can solve this problem ? 谁能给我一些思路,我可以解决这个问题? Many Many Many Thanks ~ 很多很多很多谢谢〜

I has been done my Code without Problem, this is the code below for everyone who need. 我已经完成了我的代码,没有问题,这是以下每个需要的代码。

The code to get Token from Https API 从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 "";
    }

The Method of POST 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;
    }

The code to using WECHAT WORK for sending Message 使用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);
    }

The Button Event for active Send Message Function 激活发送消息功能的按钮事件

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

        MSG.SendMsg2();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM