欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java微信公眾號開發(fā)之通過微信公眾號獲取用戶信息

 更新時間:2017年05月18日 15:16:44   作者:南=子  
這篇文章主要介紹了Java微信公眾號開發(fā)之通過微信公眾號獲取用戶信息,需要的朋友可以參考下

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

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

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

          

            

            

                             到此微信配置完畢,接下來就是直接上代碼了

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

      先說第一種

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

    https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxxxxxxx&redirect_uri=http://xxxxxx/open/openid&response_type=code&scope=snsapi_base

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

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;
    }
  }
  //做服務器校驗
  @RequestMapping("/tovalid")
  public void valid(String echostr,HttpServletResponse res) throws IOException{
    PrintWriter out = res.getWriter();
    out.print(echostr);
  }
}

第二種

    (1)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx&redirect_uri=http:// 域名

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

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);
    }
  }
}
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方式請求  24      
      http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      http.setDoOutput(true); 
      http.setDoInput(true);
      System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒28   
      System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時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微信公眾號開發(fā)之通過微信公眾號獲取用戶信息,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • Java springboot探究配置文件優(yōu)先級

    Java springboot探究配置文件優(yōu)先級

    在springboot項目中,我們可以通過在yml文件中設置變量,再通過@Value注解來獲得這個變量并使用,但如果這個項目已經(jīng)部署到服務器上,我們想更改這個數(shù)據(jù)了需要怎么做呢,其實在springboot項目中,配置文件是有優(yōu)先級的
    2023-04-04
  • 一文詳解Java8中的方法引用與構造器引用

    一文詳解Java8中的方法引用與構造器引用

    這篇文章主要為大家詳細介紹了Java8中的方法引用與構造器引用的具體用法,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下
    2023-02-02
  • struts2的國際化實現(xiàn)網(wǎng)站整體中英文切換實例代碼

    struts2的國際化實現(xiàn)網(wǎng)站整體中英文切換實例代碼

    本篇文章主要介紹了struts2的國際化實現(xiàn)網(wǎng)站整體中英文切換實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java的字符串中對子字符串的查找方法總結

    Java的字符串中對子字符串的查找方法總結

    這篇文章主要介紹了Java的字符串中對子字符串的查找方法總結,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-11-11
  • 關于SpringSecurity的基本使用示例

    關于SpringSecurity的基本使用示例

    這篇文章主要介紹了關于SpringSecurity的基本使用示例,SpringSecurity 本質是一個過濾器鏈SpringSecurity 采用的是責任鏈的設計模式,它有一條很長的過濾器鏈,需要的朋友可以參考下
    2023-05-05
  • java中文及特殊字符的校驗方法

    java中文及特殊字符的校驗方法

    這篇文章主要為大家詳細介紹了java中文及特殊字符的校驗方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • spring-boot讀取props和yml配置文件的方法

    spring-boot讀取props和yml配置文件的方法

    本篇文章主要介紹了spring-boot讀取props和yml配置文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • java中BCryptPasswordEncoder密碼的加密與驗證方式

    java中BCryptPasswordEncoder密碼的加密與驗證方式

    這篇文章主要介紹了java中BCryptPasswordEncoder密碼的加密與驗證方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • java編程數(shù)據(jù)類型全面詳解教程新手必入

    java編程數(shù)據(jù)類型全面詳解教程新手必入

    這篇文章主要為大家介紹了java編程數(shù)據(jù)類型全面詳解教程,強烈推薦新手入,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • SpringBoot中WEB的啟動流程分析

    SpringBoot中WEB的啟動流程分析

    今天我們就來分析下springboot啟動web項目整個流程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-03-03

最新評論