码迷,mamicode.com
首页 > 微信 > 详细

如何获取微信用户的名字等信息

时间:2015-04-29 10:01:48      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:微信   api   

package com.aa.util;

import org.apache.log4j.Logger;

import com.aa.servlets.WeixinServlet;

import net.sf.json.JSONObject;

public class GetUserNickNameUtil {
	
	public static String getUserNickNameTest(String OPENID) {
                //注意:这里ACCESS_TOKEN是用测试号获取的,OPENID是fromUserName
                String ACCESS_TOKEN=GetAccess_TokenUtil.getAccess_Token();
		String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ACCESS_TOKEN+"&openid="+OPENID;
		String json = HttpRequest.httpStringRequest(url, "GET", null);
		JSONObject jn=JSONObject.fromObject(json);
//		System.out.println(jn.get("nickname"));
		return jn.get("nickname").toString();
	}

}


package com.aa.util;

import net.sf.json.JSONObject;

public class GetAccess_TokenUtil {

    public static String getAccess_Token(){
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        url=url.replace("APPID", "yourAPPID").replace("APPSECRET", "yourAPPSECRET");
        JSONObject json=HttpRequest.httpRequest(url, "GET", null);
        String access_token=json.getString("access_token");
        return access_token;
    }

}


package com.aa.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;



import net.sf.json.JSONObject;

public class HttpRequest {
//        private static final Logger logger=Logger.getLogger(MenuUtil.class);
        public static final String access_tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        
        public static JSONObject httpsRequest( String requestUrl,String requestMethod,String message){
            JSONObject json=null;
            //得到请求的地址
            try {
                TrustManager[] tm={new MyX509TrustManager()};
                SSLContext ssl=SSLContext.getInstance("SSL", "SunJSSE");
                ssl.init(null, tm,new java.security.SecureRandom());
                SSLSocketFactory ssf=ssl.getSocketFactory();
                URL url=new URL(requestUrl);
                HttpsURLConnection http=(HttpsURLConnection)url.openConnection();
                http.setSSLSocketFactory(ssf);
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setUseCaches(false);
                
                http.setRequestMethod(requestMethod);
                
                System.setProperty("sun.net.client.defaultConnectionTimeout", "30000");
                System.setProperty("sun.net.client.defaultReadTimeout", "30000");
                if(message!=null){
                    OutputStream outputStream=http.getOutputStream();
                    outputStream.write(message.getBytes("UTF-8"));
                    outputStream.close();
                    
                }
                
                InputStream is=http.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(is,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                String str=null;
                StringBuffer buffer=new StringBuffer();
                while((str=bufferedReader.readLine())!=null){
                    buffer.append(str);
                }
                
                inputStreamReader.close();
                bufferedReader.close();
                is.close();
                is=null;
                http.disconnect();
                json=JSONObject.fromObject(buffer.toString());
            } catch (Exception e) {
                
                e.printStackTrace();
            } 
            return json;
        }
        public static JSONObject httpRequest( String requestUrl,String requestMethod,String message){
            JSONObject json=null;
            //得到请求的地址
            try {
                URL url=new URL(requestUrl);
                HttpURLConnection http=(HttpURLConnection)url.openConnection();
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setUseCaches(false);
                
                http.setRequestMethod(requestMethod);
                if(message!=null){
                    OutputStream outputStream=http.getOutputStream();
                    outputStream.write(message.getBytes("UTF-8"));
                    outputStream.close();
                    
                }
                
                InputStream is=http.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(is,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                String str=null;
                StringBuffer buffer=new StringBuffer();
                while((str=bufferedReader.readLine())!=null){
                    buffer.append(str);
                }
                
                inputStreamReader.close();
                bufferedReader.close();
                is.close();
                is=null;
                http.disconnect();
                json=JSONObject.fromObject(buffer.toString());
            } catch (Exception e) {
                
                e.printStackTrace();
            } 
            return json;
        }
        
        public static String httpStringRequest( String requestUrl,String requestMethod,String message){
            String result="";
            //得到请求的地址
            try {
                URL url=new URL(requestUrl);
                HttpURLConnection http=(HttpURLConnection)url.openConnection();
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setUseCaches(false);
                
                http.setRequestMethod(requestMethod);
                if(message!=null){
                    OutputStream outputStream=http.getOutputStream();
                    outputStream.write(message.getBytes("UTF-8"));
                    outputStream.close();
                    
                }
                
                InputStream is=http.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(is,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                String str=null;
                StringBuffer buffer=new StringBuffer();
                while((str=bufferedReader.readLine())!=null){
                    buffer.append(str);
                }
                
                inputStreamReader.close();
                bufferedReader.close();
                is.close();
                is=null;
                http.disconnect();
        //        json=JSONObject.fromObject(buffer.toString());
                result=buffer.toString();
            } catch (Exception e) {
                
                e.printStackTrace();
            } 
            return result;
        }
        
        
}
class MyX509TrustManager implements X509TrustManager{

    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        // TODO Auto-generated method stub
        
    }

    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        // TODO Auto-generated method stub
        
    }

    public X509Certificate[] getAcceptedIssuers() {
        // TODO Auto-generated method stub
        return null;
    }
}



 



如何获取微信用户的名字等信息

标签:微信   api   

原文地址:http://blog.csdn.net/u013863751/article/details/45350107

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!