.NET微信公众号查看关注者接口_实用技巧

本文实例为大家分享了java获取不同路径的方法,供大家参考,具体内容如下

实体类:

 public class userlist
  {
    public string total { get; set; }
    public string count { get; set; }
    public userlistopenid data { get; set; }
    public string next_openid { get; set; }
  }
 public class userlistopenid
  {
    public List<string> openid { get; set;
  }
 public class userdetail
  {
    public int subscribe { get; set; }
    public string openid { get; set; }
    public string nickname { get; set; }
    public int sex { get; set; }
    public string language { get; set; }
    public string city { get; set; }
    public string province { get; set; }
    public string country { get; set; }
    public string headimgurl { get; set; }
    public int subscribe_time { get; set; }
    public string unionid { get; set; }
    public string remark { get; set; }
    public int groupid { get; set; }
    public int[] tagid_list { get; set; }
  }

getUser.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="getUser.aspx.cs" Inherits="MyTest.WebUI.Manager.usermsg.getUser" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <!-- Bootstrap -->
  <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
  <!--[if lt IE 9]>
   <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
   <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body>
  <form runat="server">

    <div class="container">
      <div class="row">
        <div class="col-md-6 col-md-push-2">
          <asp:Button class="btn btn-primary" ID="btnGet" runat="server" Text="获取所有用户openID" OnClick="btnGet_Click" />
        </div>
        <div class="col-md-6 col-md-pull-2">
          <asp:DropDownList CssClass="form-control" ID="ddlopenids" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlopenids_SelectedIndexChanged"></asp:DropDownList>
          <asp:Label ID="lblMSG" runat="server" Text=""></asp:Label>
          <asp:Image class="img-circle" ID="imgHead" runat="server" Width="180px" Height="180px" />
        </div>
      </div>
    </div>

    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </form>
</body>
</html>
 public partial class getUser : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //获取用户列表
    protected void btnGet_Click(object sender, EventArgs e)
    {
      string next_opid = string.Empty;
      string url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+mainArg.get_Token()+"&next_openid=";
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
      using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
      {
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string result = sr.ReadToEnd();
        sr.Close();

        MyTest.Common.Entity.userlist userlist = MyTest.Common.Util.JsonEntityExchange<MyTest.Common.Entity.userlist>.Json2Entity(result);
        //Response.Write(userlist.count + "/"+userlist.data+"/"+userlist.next_openid+"/"+userlist.total);
        ddlopenids.DataSource = userlist.data.openid;
        ddlopenids.DataTextField = "";
        ddlopenids.DataValueField = "";
        ddlopenids.DataBind();
        ListItem item = new ListItem();
        item.Text = "--请选择用户的openID--";
        item.Value = "--0--";
        ddlopenids.Items.Insert(0, item);

      }
    }

    //获取用户基本信息(包括UnionID机制)
    protected void ddlopenids_SelectedIndexChanged(object sender, EventArgs e)
    {
      string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ mainArg.get_Token() + "&openid="+ddlopenids.SelectedItem.Text+"〈=zh_CN ";
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
      using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
      {
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string result = sr.ReadToEnd();
        sr.Close();
        MyTest.Common.Entity.userdetail user= MyTest.Common.Util.JsonEntityExchange<MyTest.Common.Entity.userdetail>.Json2Entity(result);

        lblMSG.Text = user.subscribe + "/" + user.openid + "/" + user.nickname + "/";
        imgHead.ImageUrl = user.headimgurl;

      }
    }
  }

mainArg.cs获取accessToken帮助类:

 public class mainArg
  {
    //测试号
    public static string appid = "wx3eb5bf1290db2ca0";
    public static string secret = "e6013be0a7338c7d3e02877db116e231";

    public string jsapi_ticket;
    public string noncestr;
    public long timestamp;
    public string signature;

    private static string path = HttpContext.Current.Server.MapPath(@"~/TemplePath");
    private static string tokenPath = HttpContext.Current.Server.MapPath(@"~/TemplePath/token.txt");
    private static string ticketPath = HttpContext.Current.Server.MapPath(@"~/TemplePath/ticket.txt");

    public mainArg() {
      noncestr = getNonceStr();
      timestamp = getTime();
    }
    /// <summary>
    /// 获取access_token
    /// </summary>
    /// <returns></returns>
    public static string get_Token()
    {
      string token = null;
      //判断是否存在或过期
      if (File.Exists(tokenPath)) {
        FileStream fs = new FileStream(tokenPath, FileMode.Open);
        var serializer = new DataContractJsonSerializer(typeof(AccToken));
        AccToken readJSToken = (AccToken)serializer.ReadObject(fs);
        fs.Close();
        FileInfo fi = new FileInfo(tokenPath);
        if (CheckTimeOut(fi.LastWriteTime) < (readJSToken.expires_in-200)) {

          return token = readJSToken.access_token;
        }

      }

      string url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
      string urlarg = @"appid=" + appid + @"&secret=" + secret;
      //      HttpUtility.UrlEncode(appid,Encoding.GetEncoding("utf-8"));
      url += urlarg;
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
      using (WebResponse response = req.GetResponse())
      {
        Stream s = response.GetResponseStream();
        StreamReader sr = new StreamReader(s);
        token = sr.ReadToEnd();

        if (!Directory.Exists(path))
        {
          Directory.CreateDirectory(path);
        }
        if (File.Exists(tokenPath))
        {
          File.Delete(tokenPath);
        }
        FileStream fs = File.Create(tokenPath);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(token);
        sw.Flush();
        sw.Close();
        fs.Close();
        FileStream fs1 = new FileStream(tokenPath, FileMode.Open);
        var serializer = new DataContractJsonSerializer(typeof(AccToken));
        AccToken readJSToken = (AccToken)serializer.ReadObject(fs1);
        fs1.Close();
        token = readJSToken.access_token;
        return token;
      }

    }
    /// <summary>
    /// 获取ticket
    /// </summary>
    /// <returns></returns>
    public string getTicket() {
      string ticket = null;
      // 判断是否存在或过期
      if (File.Exists(ticketPath))
      {
        FileStream fs = new FileStream(ticketPath, FileMode.Open);
        var serializer = new DataContractJsonSerializer(typeof(JsTicket));
        JsTicket readJSTicket = (JsTicket)serializer.ReadObject(fs);
        fs.Close();
        FileInfo fi = new FileInfo(ticketPath);
        if (CheckTimeOut(fi.LastWriteTime) < (readJSTicket.expires_in - 200))
        {

          return ticket = readJSTicket.ticket;
        }

      }

      string url = @"https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&";
      string urlarg = @"access_token="+get_Token();
      //      HttpUtility.UrlEncode(appid,Encoding.GetEncoding("utf -8"));
      url += urlarg;
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
      using (WebResponse response = req.GetResponse())
      {
        Stream s = response.GetResponseStream();
        StreamReader sr = new StreamReader(s);
        ticket = sr.ReadToEnd();

        if (!Directory.Exists(path))
        {
          Directory.CreateDirectory(path);
        }
        if (File.Exists(ticketPath))
        {
          File.Delete(ticketPath);
        }
        FileStream fs = File.Create(ticketPath);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(ticket);
        sw.Flush();
        sw.Close();
        fs.Close();
        FileStream fs1 = new FileStream(ticketPath, FileMode.Open);
        var serializer = new DataContractJsonSerializer(typeof(JsTicket));
        JsTicket readJSTicket = (JsTicket)serializer.ReadObject(fs1);
        fs1.Close();
        ticket = readJSTicket.ticket;
        return ticket;
      }

    }
    //
    public static long getTime() {
      return Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
    }
    public static string getTimeString(DateTime dt)
    {
      TimeSpan ts = dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
      return Convert.ToInt64(ts.TotalSeconds).ToString();
    }
    //获取16位随机字符串
    public static string getNonceStr()
    {
      int length = 16;
      string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      string str = "";
      Random rad = new Random();
      for (int i = 0; i < length; i++)
      {
        str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
      }
      return str;
    }

    //得到签名
    public string getSign() {

      jsapi_ticket = getTicket();
      string s1 = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url=http://winsee.imwork.net/Manager/Main/testjs.aspx", jsapi_ticket, noncestr, timestamp.ToString());
      signature = GetSHA1(s1);
      return signature;
    }
    public static string GetSHA1(string strSource)
    {
      string strResult = string.Empty;

      System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
      byte[] bytResult = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource));
      for (int i = 0; i < bytResult.Length; i++)
      {
        strResult = strResult + bytResult[i].ToString("x2");
      }
      return strResult;
    }
    //SHA1哈希加密算法
    public static string GetSHA1_1(string str_sha1_in)
    {
      SHA1 sha1 = new SHA1CryptoServiceProvider();
      byte[] bytes_sha1_in = Encoding.Default.GetBytes(str_sha1_in);
      byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
      string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
      str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
      return str_sha1_out;
    }

    // 判断是否超过7200s
    public static long CheckTimeOut(DateTime changeTime)
    {
      return Convert.ToInt64((DateTime.Now - changeTime).TotalSeconds);

    }

  }

 # region 创建Json序列化 及反序列化类目
  //创建JSon类 保存文件 ticket.txt
  public class AccToken
  {

    public string access_token { get; set; }
    public long expires_in { get; set; }
  }

  //创建从微信返回结果的一个类 用于获取ticket
  public class JsTicket
  {

    public long errcode { get; set; }
    public string errmsg { get; set; }
    public string ticket { get; set; }
    public long expires_in { get; set; }
  }
  #endregion 

JSon序列化,反序列化

public class JsonEntityExchange<T> where T:new()
  {
    /// <summary>
    /// json转实体LIST
    /// </summary>
    /// <param name="JsonStr"></param>
    /// <returns></returns>
    public static List<T> Json2Entitys(string JsonStr)
    {
      JavaScriptSerializer Serializer = new JavaScriptSerializer();
      List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
      return objs;
    }
    /// <summary>
    /// json转实体
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public static T Json2Entity(string json)
    {
      T obj = Activator.CreateInstance<T>();
      using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
      {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
        return (T)serializer.ReadObject(ms);
      }
    }
    /// <summary>
    /// 实体转json
    /// </summary>
    /// <param name="lists">实体list</param>
    /// <returns></returns>
    public static string Entity2Json(List<T> lists) {

      return new JavaScriptSerializer().Serialize(lists);
    }
  }

结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索微信关注公众号接口、微信公众账号关注接口、微信公众号js关注接口、关注公众号接口、微信公众平台关注接口,以便于您获取更多的相关知识。

时间: 2024-12-24 02:47:17

.NET微信公众号查看关注者接口_实用技巧的相关文章

ASP.NET微信公众号查看粉丝信息接口_实用技巧

本文实例为大家分享了ASP.NET微信粉丝信息接口查看代码,供大家参考,具体内容如下 微信Token实体类: /// <summary> /// 微信Token实体类 /// </summary> public class WeChatTokenEntity { public string Access_token { get; set; } public string Expires_in { get; set; } } 用户信息实体类 /// <summary> /

ASP.NET MVC5+EF6+EasyUI后台管理系统 微信公众平台开发之消息管理_实用技巧

前言  回顾上一节,我们熟悉的了解了消息的请求和响应,这一节我们来建立数据库的表,表的设计蛮复杂  你也可以按自己所分析的情形结构来建表  必须非常熟悉表的结果才能运用这张表,这表表的情形涵盖比较多  思维导图  我这个人比较喜欢用思维导图来分析和表达一些模型:   表结构  根据思维导图,我们可以建立的表可以是3张表:消息表,规则表,类型表 消息表:实际的消息 规则表:文本.图文.语音等 类型表:文本.图文.语音(默认回复,订阅回复) 也可以是两张表:规制表,消息表(+一个类型字段)  我这里

微信公众号图片上传-微信公众号上传多媒体文件接口用JAVA怎么实现

问题描述 微信公众号上传多媒体文件接口用JAVA怎么实现 根据图片的描述是通过发送https请求上传图片.微信公众号提供的文档是通过curl命令来实现发送https post请求,但是我现在的程序是通过java程序来发送https post请求.以下是我的3个问题: 1.既然是发送https post请求上传图片,java应该也可以实现,我只实现过java https post的简单请求,没有实现过java https post图片上传.有大神能帮帮我吗? 2.既然微信公众号文档上说是通过cur

二维码-微信公众号 JS-SDK 扫码接口,扫码跳转直接限制结果问题

问题描述 微信公众号 JS-SDK 扫码接口,扫码跳转直接限制结果问题 JS-SDK的 wx.scanQRCode扫码接口,代码如下: wx.scanQRCode({ needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果, scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有 success: function (res) { var result = res.resultS

微信公众号怎么取消关注 微信公众号取消关注方法详解

给各位微信软件的使用者们来详细的解析分享一下微信公众号取消关注的方法. 方法分享: 1.登陆微信,打开底部通讯录--选择公众号.   2.打开要取消关注的那个公众号.   3.点击右上角的人头像.   4.再次点击右上角的标识.   5.选择不再关注.   6.确定不再关注,即成功取消对该公众号的关注.   好了,以上的信息就是小编给各位微信的这一款软件的使用者们带来的详细的微信公众号取消关注的方法解析分享的全部内容了,各位看到这里的使用者们小编相信大家现在那是非常的清楚了取消方法了吧,那么各位

微信公众号可通过现金红包接口发放微信支付现金红包(附开发教程)

农历新年将至,支付宝红包打了一仗,微信在朋友圈屏蔽了它的分享,但单防守还不行,进攻才是最好的防守.昨日,微信支付现金红包接口正式开放,只需开通微信支付,即可接入现金红包.微信公众号也可以发放现金红包了! 通过现金红包接口,公众号开发者可以策划相关运营活动,向用户发放微信支付现金红包,更好的达到品牌推广及回馈用户的效果. 1.商户调用接口时,通过指定发送对象以及发送金额的方式发放红包,这样的方式,允许商户灵活的应用于各种各样丰富的活动场景 2.领取到红包后,用户的资金直接进入微信零钱,避免繁复的领

ASP.NET微信公众号客服接口_实用技巧

本文实例为大家分享了ASP.NET微信客服接口的具体代码,供大家参考,具体内容如下 Kf_account.cs代码: public partial class Kf_account : Form { private readonly DataTable adt_user = new DataTable(); private readonly string as_INIFile = Application.StartupPath + "\\user.ini"; public Kf_acc

微信公众号开发客服接口实例代码

最近,开发微信公众号,负责开发客服功能,这里简单记录下: Kf_account.cs代码: public partial class Kf_account : Form { private readonly DataTable adt_user = new DataTable(); private readonly string as_INIFile = Application.StartupPath + "\\user.ini"; public Kf_account() { Bind

.NET微信公众号开发之创建自定义菜单_javascript技巧

一.前言 开发之前,我们需要阅读官方的接口说明文档,不得不吐槽一下,微信的这个官方文档真的很烂,但是,为了开发我们需要的功能,我们也不得不去看这些文档. 接口文档地址:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html 看了这些个文档,基本意思明白了,就是我们把我们要创建的菜单创建好,post到微信的服务器上面,微信服务器然后给我们一些状态码,从而判断我们的菜单是否创建成功,只是在发送json数据以前我们要做