c#微信群发消息

首先你要有一个服务器,按照微信的开发者平台的要求建立一个网站

首先你要先申请一个微信,或者免费绑定一个微信的测试号,我是个穷学生,所以就用微信测试号

放一个测试号的登录连接点击打开链接   http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

反正无论怎样,你要得到一个Access_Token(这个就是你的身份信息)

如果群发消息先要干什么呢?

要得到用户列表:

先看看这个教程:点击打开链接    http://mp.weixin.qq.com/wiki/3/17e6919a39c1c53555185907acf70093.html

为了方便大家给个例子:https://api.weixin.qq.com/cgi-bin/user/get?access_token=JDcne2UsxAeQcPL7cDP7Xr7AhIUnI3RcHOEsZ_7OLw0WyldU7mAEsR7QTCvLp5f8kjYXTW-uafLqT_5LtfZp3-tI_-eSbO0sjdNVA_UWE9E&next_openid=

那个next_openid是个空值,这样微信会默认给你所有的用户列表

向微信服务器发送这个消息之后,发现里面有个json要解析,怎么办

这里要用一个Newtonsoft.Json这个东西,这个要在网上下载点击打开链接 http://download.csdn.net/download/5653325/4932275

教程呢:http://blog.csdn.net/mazhaojuan/article/details/8592969 这个是马同学的教程

好:重点来了:如何得到里面的用户列表呢?

首先要引用这两个东西:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

这个代码:(原谅我,我只会c#)

           

string geturl =
                "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+Access_Token+"&openid=";//用来获取用户列表的json的数据
            ///以下是解析
            string[] openidlist = new string[1000];///1000一个字符串
            getres = getGetPage(geturl);//得到json数据
            JObject jObjectstr = (JObject)JsonConvert.DeserializeObject(getres);///把json数据转换成jobject
            JObject datastr = (JObject) jObjectstr["data"];//得到data对象的数据
            JArray openid = (JArray) datastr["openid"];//得到openid对象的数组
            for (int i = 0; i < openid.Count; i++)///提取数组的数据转换成字符串数组
            {
                openidlist[i] = openid[i].ToString();
                sendmessage(openidlist[i],Text1.Value);这个函数大家不要着急,这是我自己写的,一会贴全部代码
            }

获得用户列表之后就可以发消息了

发消息的代码

public void sendmessage(string openida,string senddata)发送微信的消息
        {
             string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Access_Token;//发送地址
             string postData = "{\"touser\":\"" + openida + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + senddata + "\"}}";//发送消息的字符串 openida为openid,senddata为发送内容
             GetPage(posturl, postData);//以post的形式发送出去
        }

好了,亲们,我把所有的源代码贴出来,就这样,学了一点点东西记录一下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Senparc.Weixin;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP;
using System.IO;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace niexinming
{
    public partial class WebForm1 : System.Web.UI.Page
    {
      
      string Access_Token =
          "JDcne2UsxAeQcPL7cDP7Xr7AhIUnI3RcHOEsZ_7OLw0WyldU7mAEsR7QTCvLp5f8kjYXTW-uafLqT_5LtfZp3-tI_-eSbO0sjdNVA_UWE9E";这是Access_Token

      public string GetPage(string posturl, string postData)///向微信服务器发送post请求(主要是发送消息)
       {
           Stream outstream = null;
           Stream instream = null;
           StreamReader sr = null;
           HttpWebResponse response = null;
           HttpWebRequest request = null;
           Encoding encoding = Encoding.UTF8;
           byte[] data = encoding.GetBytes(postData);
           // 准备请求...  
           try
           {
               // 设置参数  
               request = WebRequest.Create(posturl) as HttpWebRequest;
               CookieContainer cookieContainer = new CookieContainer();
               request.CookieContainer = cookieContainer;
               request.AllowAutoRedirect = true;
               request.Method = "POST";//post的形式
               request.ContentType = "application/x-www-form-urlencoded";
               request.ContentLength = data.Length;
               outstream = request.GetRequestStream();
               outstream.Write(data, 0, data.Length);
               outstream.Close();
               //发送请求并获取相应回应数据  
               response = request.GetResponse() as HttpWebResponse;
               //直到request.GetResponse()程序才开始向目标网页发送Post请求  
               instream = response.GetResponseStream();
               sr = new StreamReader(instream, encoding);
               //返回结果网页(html)代码  
               string content = sr.ReadToEnd();
               string err = string.Empty;
               return content;
           }
           catch (Exception ex)
           {
               string err = ex.Message;
               Response.Write(err);
               return string.Empty;
           }

       }


        public string getGetPage(string posturl)///向微信服务器发送get请求(主要获取用户列表)
        {
           
          // string response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
          
            // 准备请求...  
            try
            {
                // 设置参数  
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                
                //发送请求并获取相应回应数据  
                Stream htmlstream = request.GetResponse().GetResponseStream();
                string sHtml = new StreamReader(htmlstream, System.Text.Encoding.Default).ReadToEnd();
                //直到request.GetResponse()程序才开始向目标网页发送get请求  
               
                //返回结果网页(html)代码  
               
                string err = string.Empty;
                return sHtml;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                Response.Write(err);
                return string.Empty;
            }
        }

        public void sendmessage(string openida,string senddata)发送微信的消息
        {
             string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Access_Token;//发送地址
             string postData = "{\"touser\":\"" + openida + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + senddata + "\"}}";//发送消息的字符串 openida为openid,senddata为发送内容
             GetPage(posturl, postData);//以post的形式发送出去
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)///这是一个按钮
        {
           
            
            string res = "";
            string getres = "";
          
           
              string geturl =
                "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+Access_Token+"&openid=";//用来获取用户列表的json的数据
            ///以下是解析
            string[] openidlist = new string[1000];///1000一个字符串
            getres = getGetPage(geturl);//得到json数据
            JObject jObjectstr = (JObject)JsonConvert.DeserializeObject(getres);///把json数据转换成jobject
            JObject datastr = (JObject) jObjectstr["data"];//得到data对象的数据
            JArray openid = (JArray) datastr["openid"];//得到openid对象的数组
            for (int i = 0; i < openid.Count; i++)///提取数组的数据转换成字符串数组
            {
                openidlist[i] = openid[i].ToString();
                sendmessage(openidlist[i],Text1.Value);
            }

            Label1.Text = getres;//在label中显示
            Response.Write(res);
            
        }
    }
}

对了,新建的是asp.net空网站

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="niexinming.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <input id="Text1" type="text" runat="server" /></div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="群发吧骚年" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>
 

愉快的群发吧






  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值