簡體   English   中英

c#URL POST Web API

[英]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 Server獲得有效令牌是成功的,

下面的代碼是我想向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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM