天天看点

微信根据openId查询微信用户信息,需要认证服务号。

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

public static JSONObject getWechatUserInfo(String openId,String taken){

String url = WechatAppConfig.wechat_tx_url+"/cgi-bin/user/info?access_token="+taken+"&openid="+openId;

HttpClient client = new HttpClient();

GetMethod get = new GetMethod(url);

       try {

        client.executeMethod(get);

        String returnJson=get.getResponseBodyAsString();

        returnJson = new String(returnJson.getBytes("ISO8859_1"),"UTF-8");

        JSONObject result = JSONObject.fromObject(returnJson);

        if(result.containsKey("nickname"))

        return new JsonResult(true).putObject(result).getResult();

        else

        return new JsonResult(false,result.optString("errcode")+":"+result.optString("errmsg")).getResult();

       }

       catch (Exception e) {

        return new JsonResult(false,e.getMessage()).getResult();

       }

return token;

}

继续阅读