调用微信接口发送消息

region 微信接口

    ///  
    /// 请求Url,发送json数据 
    ///  
    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;
    }

    /// 
    /// 获取密钥
    /// 
    /// appID
    /// secID
    /// 
    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;
    }

    /// 
    /// 填充数据后返回json字符串
    /// 
    /// 
    /// 
    /// 
    /// 
    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;
    }

    /// 
    /// 发送微信消息
    /// 
    /// 用户
    /// 标题
    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 

你可能感兴趣的:(调用微信接口发送消息)