package cn.hy.weixin.util; import net.sf.json.JSONObject; public class CommonUtil { public static String OPENID_ID = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code"; /** * 根据code获取openid * @param code * @return */ public static String getOpenId(String code){ String openid = null; String url = OPENID_ID.replace("APPID", HttpUtil.APPID).replace("APPSECRET", HttpUtil.APPSECRET).replace("CODE", code); JSONObject jsonObject = HttpUtil.doGetRequest(url); if(jsonObject != null){ openid = jsonObject.getString("openid"); } return openid; } }
package cn.hy.weixin.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * http工具类 * */ public class HttpUtil { public static final String APPID = "xxx"; public static final String APPSECRET = "xxx"; public static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; /** * 封装get请求 * @param url * @return */ public static JSONObject doGetRequest(String url){ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpClient newhttpClient = WebClientDevWrapper.wrapClient(httpClient); System.out.println("url**************" + url); HttpGet httpGet = new HttpGet(url); JSONObject jsonObject = null; try { HttpResponse response = newhttpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(result); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } /** * 封装post请求 * @param url * @param prarm * @return */ public static JSONObject doPostRequest(String url,String prarm){ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpClient newhttpClient = WebClientDevWrapper.wrapClient(httpClient); HttpPost httpPost = new HttpPost(url); JSONObject jsonObject = null; try { httpPost.setEntity(new StringEntity(prarm, "UTF-8")); HttpResponse response = newhttpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(result); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } /** * 发送短息 post请求并返回结果 */ public static String post(String url,String reqEncoding,String respEncoding,List<NameValuePair> param) { HttpClient httpclient=new DefaultHttpClient(); String resStr = ""; // 创建httppost HttpPost httppost = new HttpPost(url); // 创建参数队列 List<NameValuePair> formparams = param; UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, reqEncoding); httppost.setEntity(uefEntity); HttpResponse response; response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { resStr = EntityUtils.toString(entity,respEncoding); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 httpclient.getConnectionManager().shutdown(); } return resStr; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
获取微信openid必要的工具类,获取微信openid必要的工具类。
php获取微信code.openid.名字和头像,自己被坑了那么久,上传记录下
微信公众号获取openid与基本信息源码,获取微信公众号openid,并根据公众号openid获取微信用户名用户信息与头像等,简单易懂。
本代码作用于前后端分离获取微信openId,调用说明:在api中新建一个Default.aspx页面,复制文件中代码到页面中,修改appid,key,webAddr值。部署api;前端跳转url:api地址+“/efault.aspx?reurl="+前端页面地址;如果...
c#获取微信公众号openid,并根据公众号openid获取微信用户名用户信息与头像等,简单易懂。
微信h5授权获取用户openId的方法和步骤,用于用户登录和注册,代码已经封装好,下载就能用
微信开发获取openId
很简单的方法获取微信openid 可用于微信登陆网页等操作。php 微信开发获取 Openid 封装类
在微信公众号中,获取关注用户的OPENID。代码是用C#来进行开发的。
代码是用asp独立完成编写的,已成功运用在微信活动项目中,编写过程中搜不到具体的案例方法也是很头痛,所以放出来方便大家使用借鉴(用asp做微站开发的实在太少了)。之前放出过一个版本,较之前的版本,这次新增了...
里面的源码是网上找的,但网上的源码使用没有详细说明,好多人下载后都说无法获取到OPENID,我也是费了好大劲才将其整理好,于2020年8月测试可用。并将详细使用说明写在里面了,欢迎交流学习。
本应用便于微信公众号公众号管理 自己公众号关注用户。 1.列表 显示公众号关注用户。 2. 可以以Excel形式将用户信息 发送到PC或手机上。 3. 显示微信公众号发红包 需要的用户OpenID。
两种方式获取微信用户openid,有图文介绍,代码是全的,可以直接拿来用
通过该工具,可以获取到微信openid,该方式为静默式获取。
【ASP.NET编程知识】.NET微信公众号获取OpenID和用户信息.docx
java解析的源码和小程序的源码,自己从项目中剥离出来的,肯定能用,有疑问私信,微信小程序获取各类信息(openid、session_key以及用户基本信息、解密手机号)
PHP如何获得Openid,微信公众号通过appid和appsecret获取Openid,该类实现了从微信公众平台获取code、通过code获取openid和access_token、
tp框架获取openid并登录的demo,并且成功,分享给大家
php企业微信扫码获取openid,通过获取到的openid来对接自己网站用户授权的openid进行登录。
java+Springmvc实现获取微信用户唯一标识openId并返回