无证书https请求方法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.X509Certificate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import yui.bss.mgr.base.sec.IAcctMgr;
import yui.bss.model.dto.ext.sec.AcctDtox;
import yui.bss.security.util.YUISecurityUtils;
import yui.comn.code.BaseSvcMsgCode;
import yui.comn.model.UserInfo;
import yui.comn.util.ExpUtil;
import yui.ui.web.weixin.service.model.ReturnModel;// 获取
public String testQ(String code) {
StringBuilder url = new StringBuilder();
url.append(“https://api.weixin.qq.com/sns/oauth2/access_token?");
url.append(“appid=”).append(this.wxMpService.getWxMpConfigStorage().getAppId());
url.append(”&secret=").append(this.wxMpService.getWxMpConfigStorage().getSecret());
url.append("&code=").append(code);
url.append("&grant_type=authorization_code");
HttpClient httpclient = new DefaultHttpClient();
httpclient = this.wrapClient(httpclient);
BufferedReader in = null;
String content = null;

	try {
		HttpClient client = getSecuredHttpClient(new DefaultHttpClient());
		// 实例化HTTP方法
		HttpGet request = new HttpGet();
		request.setURI(new URI(url.toString()));
		HttpResponse response = client.execute(request);

		in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		StringBuffer sb = new StringBuffer("");
		String line = "";
		String NL = System.getProperty("line.separator");
		while ((line = in.readLine()) != null) {
			sb.append(line + NL);
		}
		in.close();
		content = sb.toString();
		System.out.println(content);
		Gson gson = new GsonBuilder().create();
		JsonParser jsonParser = new JsonParser();
		JsonObject  jsonObject = jsonParser.parse(content).getAsJsonObject();
		String openid = (String)jsonObject.get("openid").getAsString();
		return openid;
	} catch (Exception e) {
	} finally {
		if (in != null) {
			try {
				in.close();// 最后要关闭BufferedReader
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	return "";
}

/**
 * 重新包装httpclient对象,忽略证书验证
 * 
 * @param httpClient
 * @return
 * @author:Administrator
 * @date:2014-9-2
 */
private static DefaultHttpClient getSecuredHttpClient(HttpClient httpClient) {
	final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
	try {
		SSLContext ctx = SSLContext.getInstance("TLS");
		X509TrustManager tm = new X509TrustManager() {
			@Override
			public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
					throws CertificateException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
					throws CertificateException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public java.security.cert.X509Certificate[] getAcceptedIssuers() {
				// TODO Auto-generated method stub
				return null;
			}
		};
		ctx.init(null, new javax.net.ssl.TrustManager[] { tm }, new SecureRandom());
		SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		ClientConnectionManager ccm = httpClient.getConnectionManager();
		SchemeRegistry sr = ccm.getSchemeRegistry();
		sr.register(new Scheme("https", 443, ssf));
		return new DefaultHttpClient(ccm, httpClient.getParams());
	} catch (Exception e) {
		System.out.println("=====:=====");
		e.printStackTrace();
	}
	return null;
}

原文:https://blog.csdn.net/achenyuan/article/details/72956292

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值