联合登录,请求令牌时对方返回空以及SSLPeerUnverifiedException的解决办法

原创 2015年07月09日 11:53:24

最近做了一个联合登录,在本地测试通过,但是一上线就报错。后来通过日志信息观察到,去请求令牌时,对方返回了空。现在把修改前后的请求方法写在下面:

之前,请求不到令牌的写法:

private static JSONObject getJsonFromUrl(String urlStr) {
		// org.apache.http.client.HttpClient
		HttpClient httpClient = new DefaultHttpClient();
		// org.apache.http.client.methods.HttpPost
		HttpPost httpPost = new HttpPost(urlStr);
		JSONObject json = null;
		try {			
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			// 使用InputStream从数据流中读取数据
			InputStream is = entity.getContent();
			byte[] bytes = new byte[256];
			StringBuffer sb = new StringBuffer();
			while (is.read(bytes) > 0) {				
				sb.append(new String(bytes, HTTP.UTF_8));
				bytes = new byte[256];
			}
			json = JSONObject.fromObject(sb.toString());
		} catch (Exception e) {
			LOG.error("http client execute error:" + e.getMessage(), e);
		}
		return json;
	}	
可以请求到令牌的写法:

private static JSONObject getJsonFromUrl(String urlStr) {
		// org.apache.http.client.HttpClient
		HttpClient httpClient = new DefaultHttpClient();
		// org.apache.http.client.methods.HttpPost
		HttpPost httpPost = new HttpPost(urlStr);
		String urlResult = "";
		try {
			// org.apache.http.util.EntityUtils	;Static helpers for dealing with HttpEntitys.	;Since:4.0
			// apache提供的方法 EntityUtils.toString : Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
			urlResult = EntityUtils.toString(httpClient.execute(httpPost).getEntity(), "UTF-8");
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("urlStr from yihaodian is : " + urlResult);

		JSONObject json = JSONObject.fromObject(urlResult);
		System.out.println("urlStr to JSONObject , JSONObject is : " + json);

		return json;
	}

具体原因,还在找

然后,又出现了如下异常:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
	at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
	at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
	at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
好在有网,查询后,得到了这样的解决方案:
/**
	* 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
	*
	* @param base
	* @return
	*/
	public static HttpClient wrapClient(HttpClient base) {
		try {
			SSLContext ctx = SSLContext.getInstance("TLS");
			X509TrustManager tm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] xcs, String string) {
				}

				public void checkServerTrusted(X509Certificate[] xcs, String string) {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			ctx.init(null, new TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx);
			ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			ClientConnectionManager ccm = base.getConnectionManager();
			SchemeRegistry sr = ccm.getSchemeRegistry();
			sr.register(new Scheme("https", ssf, 443));
			System.out.println("==[" + DateUtil.DateTime() + "]成功忽略证书有效性");
			return new DefaultHttpClient(ccm, base.getParams());
		} catch (Exception ex) {
			System.out.println("==[" + DateUtil.DateTime() + "]忽略证书有效性出现异常:" + ex.getMessage());
			ex.printStackTrace();
			return null;
		}
	}
然后,HttpClient对象,需要处理一下:

HttpClient httpClient = new DefaultHttpClient();
// 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
httpClient = wrapClient(httpClient);

说明一下需要导入的包,因为可能有的类重名,会造成困惑。可能并不是所有的包都有用,Eclipse划黄线的,删掉就好:

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;

致敬:http://www.iteye.com/topic/1136303

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

https应用:避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常

在开发https应用时,你的测试服务器常常没有一个(有效的)SSL证书。在你的客户端连接测试服务器时,如下的异常会被抛出:”javax.net.ssl.SSLPeerUnverifiedExcepti...

OkHttp调用HTTPS遇到的问题之:SSLPeerUnverifiedException

关于OkHttp OkHttp学习 Https学习 最近在用OkHttp调https接口的时候遇到一个问题 javax.net.ssl.SSLPeerUnverifiedException: pe...

https应用:避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常

在开发https应用时,你的测试服务器常常没有一个(有效的)SSL证书。在你的客户端连接测试服务器时,如下的异常会被抛出:”javax.net.ssl.SSLPeerUnverifiedExcepti...
  • atco
  • atco
  • 2013-07-04 20:53
  • 20944

NO.91 SSLPeerUnverifiedException 问题之解决(附HttpClientUtils升级版)

SSLPeerUnverifiedException问题解决。 附HttpClinet工具 1. 基于Apache HttpClient4 2. 添加获取图片的方法 3. 支持https 4. 支持会...

避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常

在开发https应用时,你的测试服务器常常没有一个(有效的)SSL证书。在你的客户端连接测试服务器时,如下的异常会被抛出:”javax.net.ssl.SSLPeerUnverifiedExcepti...

peer not authenticated和Could not generate DH keypair解决方法

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated 网上基本上都是信任所有证书来解决 SSLSocketFactory.A...
  • NK_TF
  • NK_TF
  • 2016-08-10 20:08
  • 3824

Dagger2 这次入门就不用放弃了

Dagger2 这次入门就不用放弃了前言之前也研究过很多次Dagger2这东西了,可能以后RxJava+Retrofit+MVP+Dagger2是Android发展的主流框架,看了Dagger2的实现...

HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常

在开发https应用时,你的测试服务器常常没有一个(有效的)SSL证书。在你的客户端连接测试服务器时,如下的异常会被抛出:”javax.net.ssl.SSLPeerUnverifiedExcepti...

HttpClient javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

最近在调用一些https的接口,然后采用的认证方式是用户名+密码。 这个时候调用接口则会提示httpclient未认证的错误,导致接口无法调用: HttpClient javax.net.ssl.SS...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)