1. 程式人生 > >呼叫微信介面傳送訊息

呼叫微信介面傳送訊息

region 微信介面

    /// <summary> 
    /// 請求Url,傳送json資料 
    /// </summary> 
    public static string PostUrl(string url, string postData)
    {
        byte[] data = Encoding.UTF8.GetBytes(postData);

        // 設定引數 
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        CookieContainer cookieContainer = new CookieContainer();
        request.CookieContainer = cookieContainer;
        request.AllowAutoRedirect = true;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        Stream outstream = request.GetRequestStream();
        outstream.Write(data, 0, data.Length);
        outstream.Close();

        //傳送請求並獲取相應迴應資料 
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        //直到request.GetResponse()程式才開始向目標網頁傳送Post請求 
        Stream instream = response.GetResponseStream();
        StreamReader sr = new StreamReader(instream, Encoding.UTF8);
        //返回結果網頁(html)程式碼 
        string content = sr.ReadToEnd();
        return content;
    }

    /// <summary>
    /// 獲取金鑰
    /// </summary>
    /// <param name="corpid">appID</param>
    /// <param name="corpsecret">secID</param>
    /// <returns></returns>
    public static string GetQYAccessToken(string corpid, string corpsecret)
    {
        string getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
        string accessToken = "";

        string respText = "";

        //獲取josn資料
        string url = string.Format(getAccessTokenUrl, corpid, corpsecret);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (Stream resStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(resStream, Encoding.Default);
            respText = reader.ReadToEnd();
            resStream.Close();
        }

        try
        {
            int start = respText.IndexOf("access_token");
            int end = respText.IndexOf("expires_in");
            start += 15;
            end -= 3;
            respText = respText.Substring(start, end - start);

            //通過鍵access_token獲取值
            accessToken = respText;
        }
        catch (Exception ex) { }

        return accessToken;
    }

    /// <summary>
    /// 填充資料後返回json字串
    /// </summary>
    /// <param name="touser"></param>
    /// <param name="tmpid"></param>
    /// <param name="title"></param>
    /// <returns></returns>
    public static string FillData(string touser,string tmpid,string title)
    {
        string responeJsonStr = "{";
        responeJsonStr += "\"touser\": \"" + touser + "\",";
        responeJsonStr += "\"template_id\": \"" + tmpid + "\",";
        responeJsonStr += "\"url\": \"\",";
        responeJsonStr += "\"topcolor\": \"#FF0000\",";
        responeJsonStr += "  \"data\": {";
        responeJsonStr += "  \"first\": {\"value\":\"" + title + "\",\"color\":\"#FF0000\"},";
        responeJsonStr += "  \"name\": {\"value\":\"使用者你好\",\"color\":\"#FF0000\"},";
        responeJsonStr += "  \"user\": {\"value\":\"感謝你的支援\",\"color\":\"#FF0000\"}";
        responeJsonStr += "}";
        responeJsonStr += "}";
        return responeJsonStr;
    }

    /// <summary>
    /// 傳送微信訊息
    /// </summary>
    /// <param name="user">使用者</param>
    /// <param name="title">標題</param>
    public static void SendWXmessage(string user,string title)
    {
        string mytoken = GetQYAccessToken("wx9f04b66052974517", "d4624c36b6795d1d99dcf0547af5443d");
        PostUrl(string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", mytoken), FillData("oEA4av_bAk7ew5k8qLc-kmzX7_SM", "dZetMJDGa9yr-aej8r3oxXWO3eOtlWIZD_DVddOe5mM", title));
    }
    #endregion