Home Backend Development C#.Net Tutorial ASP.NET WeChat public account viewing fan information interface

ASP.NET WeChat public account viewing fan information interface

Dec 20, 2016 pm 01:27 PM
WeChat public account

The example in this article shares the ASP.NET WeChat fan information interface viewing code for your reference. The specific content is as follows

WeChat Token entity class:

/// <summary>
/// 微信Token实体类
/// </summary>
public class WeChatTokenEntity
{
public string Access_token { get; set; }
 
public string Expires_in { get; set; }
}
Copy after login

User information entity class

/// <summary>
/// 用户实体信息类
/// </summary>
public class WeChatUserEntity
{
public string Subscribe { get; set; }
 
public string Openid { get; set; }
 
public string Nickname { get; set; }
 
public string Sex { get; set; }
 
public string City { get; set; }
 
public string Province { get; set; }
 
public string Country { get; set; }
 
public string HeadImgUrl { get; set; }
 
public string Subscribe_time { get; set; }
 
public string Language { get; set; }
}
Copy after login

WeChat auxiliary operation Class

public class WeChatDemo
{
/*
 * 步骤:
 * 1.通过appid和secret请求微信url,得到token
 * 2.通过access_token和openid得到用户信息(头像地址等)
 * 3.通过access_token和media_id得到用户发送的微信消息
 *
 */
 
 
string appId = "wxxxxxxxxxxxxxx";
string appSecret = "1234567890-==687";
 
string wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
 
 
public WeChatDemo()
{
 
}
 
/// <summary>
/// 获取token信息
/// </summary>
/// <returns></returns>
public WeChatTokenEntity GetWechatToken()
{
 //请求的url地址
 string tokenUrl = string.Format(wechatUrl, appId, appSecret);
 WeChatTokenEntity myToken;
 
 try
 {
 //声明并实例化一个WebClient对象
 WebClient client = new WebClient();
 //从执行url下载数据
 byte[] pageData = client.DownloadData(tokenUrl);
 //把原始数据的byte数组转为字符串
 string jsonStr = Encoding.Default.GetString(pageData);
 //初始化一个JavaScriptSerializer json解析器
 //序列化注意:需要引用System.Web.Extensions
 JavaScriptSerializer jss = new JavaScriptSerializer();
 //将字符串反序列化为Token对象
 myToken = jss.Deserialize<WeChatTokenEntity>(jsonStr);
 }
 catch (WebException ex)
 {
 throw ex;
 }
 catch (Exception ex)
 {
 throw ex;
 }
 
 return myToken;
}
 
/// <summary>
/// 获取用户信息
/// </summary>
/// <param name="accessToken"></param>
/// <param name="openId"></param>
/// <returns></returns>
public WeChatUserEntity GetUserIfo(string accessToken, string openId)
{
 WeChatUserEntity wue = new WeChatUserEntity();
 
 string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";
 
 url = string.Format(url, accessToken, openId);
 
 try
 {
 WebClient wc = new WebClient();
 byte[] pageData = wc.DownloadData(url);
 string jsonStr = Encoding.UTF8.GetString(pageData);
 JavaScriptSerializer jss = new JavaScriptSerializer();
 wue = jss.Deserialize<WeChatUserEntity>(jsonStr);
 
 }
 catch (WebException ex)
 {
 throw ex;
 }
 catch (Exception ex)
 {
 throw ex;
 }
 
 return wue;
}
 
public string GetVoice(string accessToken, string mediaId)
{
 string voiceAddress = string.Empty;
 string voiceUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}";
 voiceUrl = string.Format(voiceUrl, accessToken, mediaId);
 
 WebClient wc = new WebClient();
 byte[] pageData = wc.DownloadData(voiceUrl);
 string jsonStr = Encoding.UTF8.GetString(pageData);
 
 //TODO:获取声音
 voiceAddress = jsonStr;
 
 return voiceAddress;
}
 
/// <summary>
/// 时间戳转为当前时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public DateTime TimeStamp2DateTime(string timeStamp)
{
 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
 long time = long.Parse(timeStamp + "0000000");
 TimeSpan toNow = new TimeSpan(time);
 return dtStart.Add(toNow);
}
 
}
Copy after login

Main program:

class Program
{
static void Main(string[] args)
{
 WeChatDemo wcd = new WeChatDemo();
 WeChatTokenEntity wte = wcd.GetWechatToken();
 string token = wte.Access_token;
 string openId = "ogNVpt52xxxxxxxxxxxxxxxxxx";
 
 Console.WriteLine("第一步:获得access_token:\n " + token + "\n");
 
 Console.WriteLine("第二步:获得用户信息");
 WeChatUserEntity user = wcd.GetUserIfo(token, openId);
 
 Console.WriteLine("\n昵称:" + user.Nickname);
 Console.WriteLine("国家:" + user.Country);
 Console.WriteLine("省份:" + user.Province);
 Console.WriteLine("城市:" + user.City);
 Console.WriteLine("语言:" + user.Language);
 Console.WriteLine("性别:" + user.Sex);
 Console.WriteLine("OpenId:" + user.Openid);
 Console.WriteLine("是否订阅:" + user.Subscribe);
 Console.WriteLine("时间:" + wcd.TimeStamp2DateTime(user.Subscribe_time));
 Console.WriteLine("头像地址:" + user.HeadImgUrl);
 
 Console.WriteLine("\n第三步:获取微信声音地址");
 string mediaId = "vwvnskvsldkvmsdlvkmdslkvmsld";
 
 string voiceAddress = wcd.GetVoice(token, mediaId);
 Console.WriteLine("声音地址:" + voiceAddress);
 Console.Read();
}
}
Copy after login

The running result is as shown in the figure:

ASP.NET WeChat public account viewing fan information interface

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I hope everyone will support PHP. Chinese website.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Scrapy implements crawling and analysis of WeChat public account articles Scrapy implements crawling and analysis of WeChat public account articles Jun 22, 2023 am 09:41 AM

Scrapy implements article crawling and analysis of WeChat public accounts. WeChat is a popular social media application in recent years, and the public accounts operated in it also play a very important role. As we all know, WeChat public accounts are an ocean of information and knowledge, because each public account can publish articles, graphic messages and other information. This information can be widely used in many fields, such as media reports, academic research, etc. So, this article will introduce how to use the Scrapy framework to crawl and analyze WeChat public account articles. Scr

What are the differences between WeChat official account certification and non-certification? What are the differences between WeChat official account certification and non-certification? Sep 19, 2023 pm 02:15 PM

The difference between WeChat public account authentication and non-authentication lies in the authentication logo, function permissions, push frequency, interface permissions and user trust. Detailed introduction: 1. Certification logo. Certified public accounts will obtain the official certification logo, which is the blue V logo. This logo can increase the credibility and authority of the public account and make it easier for users to identify the real official public account; 2. Function permissions. Certified public accounts have more functions and permissions than uncertified public accounts. For example, certified public accounts can apply to activate the WeChat payment function to achieve online payment and commercial operations, etc.

Practical crawler combat in Python: WeChat public account crawler Practical crawler combat in Python: WeChat public account crawler Jun 10, 2023 am 09:01 AM

Python is an elegant programming language with powerful data processing and web crawling capabilities. In this digital era, the Internet is filled with a large amount of data, and crawlers have become an important means of obtaining data. Therefore, Python crawlers are widely used in data analysis and mining. In this article, we will introduce how to use Python crawler to obtain WeChat public account article information. WeChat official account is a popular social media platform for publishing articles online and is an important tool for promotion and marketing of many companies and self-media.

Use PHP to build a WeChat public account API interface Use PHP to build a WeChat public account API interface May 13, 2023 pm 12:01 PM

In today's Internet era, WeChat official accounts have become an important marketing channel for more and more companies. If you want your WeChat official account to implement more functions, you often need to write corresponding interfaces. This article will use PHP language as an example to introduce how to build a WeChat public account API interface. 1. Preparation Before writing the WeChat public account API interface, the developer needs to have a WeChat public account and apply for developer interface permissions in the WeChat public platform. After the application is successful, you can obtain the relevant developer AppID and AppSe

How to use Laravel to develop an online ordering system based on WeChat public account How to use Laravel to develop an online ordering system based on WeChat public account Nov 02, 2023 am 09:42 AM

How to use Laravel to develop an online ordering system based on WeChat official accounts. With the widespread use of WeChat official accounts, more and more companies are beginning to use them as an important channel for online marketing. In the catering industry, developing an online ordering system based on WeChat public accounts can improve the efficiency and sales of enterprises. This article will introduce how to use the Laravel framework to develop such a system and provide specific code examples. Project preparation First, you need to ensure that the Laravel framework has been installed in the local environment. OK

Can the official account only post one article per day? Can the official account only post one article per day? Jun 16, 2023 pm 02:04 PM

The public account can not only post one article per day, but can publish up to eight articles at a time. How to publish multiple articles: 1. Click "Material Management" on the left, and then click "New Graphic and Text Material" to start editing. First article; 2. After editing the first article, click the + sign under the first article on the left and click "Graphic Message" to edit the second article; 3. After finishing multiple images and text, click " Save and send in bulk" to complete the publishing of multiple articles.

Build WeChat public account application using Go language framework Build WeChat public account application using Go language framework Jun 04, 2023 am 10:40 AM

With the popularity of the Internet and the widespread use of mobile devices, WeChat official accounts have become an indispensable part of corporate marketing. Through WeChat public accounts, companies can easily interact with users, promote products and services, and increase brand awareness. In order to better develop WeChat public account applications, more and more developers and companies choose to use Go language to build WeChat public account applications. Go language is a programming language developed by Google. Its syntax is concise and suitable for building high-performance, high-concurrency real-time applications. In terms of ease of use and

PHP and WeChat public account development guide PHP and WeChat public account development guide Jun 11, 2023 pm 03:31 PM

With the gradual popularity of WeChat public accounts in social networks, more and more developers have begun to get involved in the field of WeChat public account development. Among them, PHP, as a common back-end programming language, has also begun to be widely used in the development of WeChat public accounts. This article will introduce the basic knowledge and common techniques of PHP in WeChat public account development. 1. Basics of PHP and WeChat public account development WeChat public account development WeChat public account refers to an Internet application based on the WeChat platform, which can provide users with different types of services and content, such as information push

See all articles