shiyong de shi https
org.apache.http.conn.ConnectTimeoutException: *****.com:443 timed out
solution
public class httpsclientspool{
private static final SchemeRegistry schemeRegistry = new SchemeRegistry();
private static final PoolingClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager(schemeRegistry);
private static final HttpParams params = new BasicHttpParams();
private static DefaultHttpClient client = new DefaultHttpClient(clientConnectionManager, params);
private static X509TrustManager trustManager= new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
static {
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 29000);
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
logger.error(" ssl error " , e);
}
try {
ctx.init(null, new TrustManager[] { trustManager }, null);
} catch (KeyManagementException e) {
logger.error(" trustManager " , e);
}
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
schemeRegistry.register(new Scheme("https", 443, ssf)) ;
clientConnectionManager.setMaxTotal(200);
clientConnectionManager.setDefaultMaxPerRoute(20);
}
protected String doPost(String url, List<NameValuePair> params) {
try {
logger.info("post to url=" + url);
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(uefEntity);
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (null != response && response.getEntity()!= null) {
responseText = EntityUtils.toString(entity, "UTF-8");
}
logger.info("post result=" + responseText);
if (StringUtils.isBlank(responseText)) {
throw new RuntimeException("返回为空!");
}
} catch (SocketTimeoutException timeoutException) {
responseText = "";
}catch (ConnectTimeoutException timeOut){
responseText = "";
} catch (Exception e) {
}finally{
}
return responseText;
}
done ~~