Bouffon
zjyuan1100
采纳率36.4%
2015-01-28 07:55 阅读 11.6k

.net 微信开发网页授权回调如何获取openid

50

已经获得code现在要访问
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code”
返回的JSON数据包如下:

{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}

现在要获取openid,怎么在后台访问这个链接回调后处理json数据,然后筛选出openid,直接赋给变量。
最终目的:string openid=回调后处理筛选好的openid。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    showbo GoCityPass新加坡曼谷通票 2015-01-28 08:27

    HttpWebRequest请求那个地址获取返回的json数据然后前后截取或者建立json对应的类,用Newtonsoft.Json.Net20.dll序列化为你的类就可以访问了。

     using System;
    using System.Web;
    using System.Net;
    using System.IO;
    using System.Text;
    public class HttpSend
    {
         /// <summary>
        /// GET下载页面内容
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <param name="encoding">内容编码</param>
        /// <returns></returns>
        public static string GetPageContent(string url, Encoding encoding)
        {
            string html=null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "GET";
    
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream ioStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(ioStream, encoding);
                html = sr.ReadToEnd();
                sr.Close();
                ioStream.Close();
                response.Close();
            }
            catch (Exception ex) {  }
            return html;
        }
    }
    
    
    //使用
    string html=HttpSend.GetPageContent("你的qq oauth地址",Encoding.UTF8);
    string key = "\"openid\":\"";
            int startIndex = html.IndexOf(key);
            if (startIndex != -1)
            {
                int endIndex = html.IndexOf("\",", startIndex);
                string openid = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length);
                Response.Write(openid);
            }
            else
            {//找不到openid,出错了。。
            }
    
    
    点赞 3 评论 复制链接分享
  • baidu_33009257 baidu_33009257 2016-03-26 14:57

    请问如何获取code

    点赞 2 评论 复制链接分享
  • chenxinjian007 chenxinjian007 2015-08-18 11:30

    我获取到的code怎么什么都没有呢

    点赞 评论 复制链接分享

相关推荐