suiteaccesstoken报错
Resuiteaccesstoken报错
这个问题是怎么解决的??
-------------------------
回 3楼竹梅的帖子
httpclient-4.4报错同样的错
-------------------------
Resuiteaccesstoken报错
原因分析:httpclient对https进行服务器证书ssl校验,某些情况下,URI制定一个ip而不是主机,这种情况下subjectataltname证书必须存在完全匹配URI的ip。从本质上讲该服务的证书不符合HTTPS规范,应联系改服务器建立证书与ip对应的设置。
解决办法(不推荐):
//创建TrustManager
X509TrustManager xtm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
//HOST验证
X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
public void verify(String arg0, SSLSocket arg1) throws IOException {}
public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {}
public void verify(String arg0, X509Certificate arg1) throws SSLException {}
};
try {
HttpClient httpClient=new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance('TLS');
//使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
ctx.init(null, new TrustManager[] { xtm }, null);
//创建SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
socketFactory.setHostnameVerifier(hostnameVerifier);
SSLSocketFactory.getSocketFactory().setHostnameVerifier(hostnameVerifier);
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme('https', socketFactory, 443));
//钉钉获取token
URI url = new URI('https://oapi.dingtalk.com/sso/gettoken?corpid=dingedXXXXXXXX&corpsecret=XXXXXXXXX');
HttpGet get = new HttpGet(url);
HttpResponse response = httpClient.execute(get);
HttpEntity entity=response.getEntity();
if(entity!= null){
JSONObject jsonObject=JSONObject.parseObject(EntityUtils.toString(entity));
System.out.print(jsonObject.get('access_token') );
}
} catch (Exception e) {
e.printStackTrace();
}
-------------------------
回 7楼略的帖子
导入的包
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
赞0
踩0