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捐助),沒錢捧個人場,謝謝各位。

謝謝您的贊助,我會做的更好!