一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

2020-10-06 17:21南=子 Java教程

這篇文章主要介紹了Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息,需要的朋友可以參考下

最近由于公司業(yè)務(wù),就開(kāi)始研究微信開(kāi)發(fā)的流程,說(shuō)實(shí)話,這東西剛開(kāi)始看到時(shí)候和看天書的一樣,總算,看了一天的文檔,測(cè)試代碼終于出來(lái)了。

1、首先需要到微信網(wǎng)站去設(shè)置一下,我是直接用的微信測(cè)試號(hào)。

        接口配置信息必須要填寫的,所以說(shuō)必須能將自己的服務(wù)發(fā)布出去

Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

          Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

            Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

            Java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息

                             到此微信配置完畢,接下來(lái)就是直接上代碼了

2、獲取用戶信息的方式一共是兩種,前提都是用戶關(guān)注微信公眾號(hào),一種是靜默獲取(snsapi_base,這種方式只能獲取openid),另一種是授權(quán)獲取(snsapi_userinfo,可以獲取用戶的詳細(xì)信息)。

      先說(shuō)第一種

  (1)首先需要先訪問(wèn)微信的鏈接

    

           這里的 uri就是直接回掉我們的服務(wù)地址,一定要記住,服務(wù)校驗(yàn)的判斷,我是按照來(lái)判斷的echostr(第二種方式也是這樣)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package net.itraf.controller;
import java.io.ioexception;
import java.io.inputstream;
import java.io.printwriter;
import java.net.httpurlconnection;
import java.net.malformedurlexception;
import java.net.url;
import javax.servlet.http.httpservletresponse;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import com.alibaba.fastjson.jsonobject;
@controller
@requestmapping("/open")
public class opencontroller {
  @requestmapping("/toopenid")
  public @responsebody string getopenid(string code,string echostr,httpservletresponse res) throws ioexception{
    if(echostr==null){
      string url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx24d47d2080f54c5b&secret=95011ac70909e8cca2786217dd80ee3f&code="+code+"&grant_type=authorization_code";
      system.out.println(code);
      string openid="";
      try {
        url geturl=new url(url);
        httpurlconnection http=(httpurlconnection)geturl.openconnection();
        http.setrequestmethod("get");
        http.setrequestproperty("content-type","application/x-www-form-urlencoded");
        http.setdooutput(true);
        http.setdoinput(true);
        http.connect();
        inputstream is = http.getinputstream();
        int size = is.available();
        byte[] b = new byte[size];
        is.read(b);
        string message = new string(b, "utf-8");
        jsonobject json = jsonobject.parseobject(message);
        openid = json.getstring("openid");
        } catch (malformedurlexception e) {
          e.printstacktrace();
        } catch (ioexception e) {
          e.printstacktrace();
        }
      return openid;
    }else{
      printwriter out = res.getwriter();
      out.print(echostr);
      return null;
    }
  }
  //做服務(wù)器校驗(yàn)
  @requestmapping("/tovalid")
  public void valid(string echostr,httpservletresponse res) throws ioexception{
    printwriter out = res.getwriter();
    out.print(echostr);
  }
}

第二種

    (1)

 域名

/open/openid&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package net.itraf.controller;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import net.sf.json.jsonobject;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
@controller
@requestmapping("/weixin")
public class oauth2action {
  @requestmapping("/oauth")
  public void auth(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    string echostr = request.getparameter("echostr");
    if(echostr==null){
      string appid = "wx24d47d2080f54c5b";
      string appsecret = "95011ac70909e8cca2786217dd80ee3f";
      //拼接
      string get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
          + "appid="
          + appid
          + "&secret="
          + appsecret
          + "&code=code&grant_type=authorization_code";
      string get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid&lang=zh_cn";
      request.setcharacterencoding("utf-8");
      response.setcharacterencoding("utf-8");
      string code = request.getparameter("code");
      system.out.println("******************code=" + code);
      get_access_token_url = get_access_token_url.replace("code", code);
      string json = httpsgetutil.dohttpsgetjson(get_access_token_url);
      jsonobject jsonobject = jsonobject.fromobject(json);
      string access_token = jsonobject.getstring("access_token");
      string openid = jsonobject.getstring("openid");
      get_userinfo = get_userinfo.replace("access_token", access_token);
      get_userinfo = get_userinfo.replace("openid", openid);
      string userinfojson = httpsgetutil.dohttpsgetjson(get_userinfo);
      jsonobject userinfojo = jsonobject.fromobject(userinfojson);
      string user_openid = userinfojo.getstring("openid");
      string user_nickname = userinfojo.getstring("nickname");
      string user_sex = userinfojo.getstring("sex");
      string user_province = userinfojo.getstring("province");
      string user_city = userinfojo.getstring("city");
      string user_country = userinfojo.getstring("country");
      string user_headimgurl = userinfojo.getstring("headimgurl");
      response.setcontenttype("text/html; charset=utf-8");
      printwriter out = response.getwriter();
      out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">");
      out.println("<html>");
      out.println(" <head><title>a servlet</title></head>");
      out.println(" <body>");
      out.print(" this is ");
      out.print(this.getclass());
      out.println(", using the post method \n");
      out.println("openid:" + user_openid + "\n\n");
      out.println("nickname:" + user_nickname + "\n\n");
      out.println("sex:" + user_sex + "\n\n");
      out.println("province:" + user_province + "\n\n");
      out.println("city:" + user_city + "\n\n");
      out.println("country:" + user_country + "\n\n");
      out.println("<img src=/" + user_headimgurl + "/");
      out.println(">");
      out.println(" </body>");
      out.println("</html>");
      out.flush();
      out.close();
    }else{
      printwriter out = response.getwriter();
      out.print(echostr);
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package net.itraf.controller;
import java.io.ioexception;
import java.io.inputstream;
import java.net.httpurlconnection;
import java.net.malformedurlexception;
import java.net.url;
public class httpsgetutil {
  public static string dohttpsgetjson(string url)
  {
    string message = "";
    try
    {
      system.out.println("dohttpsgetjson");//todo:dd
      url urlget = new url(url);
      httpurlconnection http = (httpurlconnection) urlget.openconnection();
      http.setrequestmethod("get");   //必須是get方式請(qǐng)求  24     
      http.setrequestproperty("content-type","application/x-www-form-urlencoded");
      http.setdooutput(true);
      http.setdoinput(true);
      system.setproperty("sun.net.client.defaultconnecttimeout", "30000");//連接超時(shí)30秒28  
      system.setproperty("sun.net.client.defaultreadtimeout", "30000"); //讀取超時(shí)30秒29 30   
      http.connect();
      inputstream is =http.getinputstream();
      int size =is.available();
      byte[] jsonbytes =new byte[size];
      is.read(jsonbytes);
      message=new string(jsonbytes,"utf-8");
    }
    catch (malformedurlexception e)
    {
       e.printstacktrace();
     }
    catch (ioexception e)
     {
       e.printstacktrace();
     }
    return message;
  }
}

以上所述是小編給大家介紹的java微信公眾號(hào)開(kāi)發(fā)之通過(guò)微信公眾號(hào)獲取用戶信息,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/yangming5423/archive/2017/05/18/6873267.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日爽| 精品亚洲麻豆1区2区3区 | 国产视频二区 | 精品国产一区二区在线观看 | 精品国产国产精2020久久日 | 日本免费三片在线观看 | 亚洲 欧美 国产 视频二区 | 免费看又黄又爽又猛的视频软件- | jk制服白丝超短裙流白浆 | 小sao货ji巴cao死你视频 | 麻豆网站视频国产在线观看 | 92精品国产成人观看免费 | 美女靠逼的视频 | 30分钟的高清视频在线观看 | 色天天综合网色鬼综合 | 亚洲色影 | 消息称老熟妇乱视频一区二区 | 日韩欧美高清 | 极致堕落(高h) | 思思91精品国产综合在线 | 俄罗斯烧性春三级k8播放 | 小柔的性放荡羞辱日记 | 欧美a一片xxxx片与善交 | 99视频在线国产 | 公交车高h| 久久精品国产亚洲AV热无遮挡 | aaaa大片| 4hu永久地域网名入口 | 不卡日本| 狠狠色婷婷丁香六月 | 国产成人夜色影视视频 | 我年轻漂亮的继坶2中字在线播放 | chinese男gay飞机同志 | 爱福利一区二区 | 欧美一级v片 | 国产精品免费小视频 | 奇米888在线看奇米999 | 国产美女在线一区二区三区 | 亚洲四虎永久在线播放 | 亚洲精品国产A久久久久久 亚洲精品福利一区二区在线观看 | 日韩精品欧美 |