如何使用HttpsUrlConnection代替DefaultHttpClient [英] how to use HttpsUrlConnection instead of DefaultHttpClient
本文介绍了如何使用HttpsUrlConnection代替DefaultHttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不推荐使用DefaultHttpClient,ThreadSafeClientConnManager,HttpParams,HttpProtocolParams,SchemeRegistry,SSLSocketFactory,NameValuePair,HttpResponse.
DefaultHttpClient, ThreadSafeClientConnManager, HttpParams,HttpProtocolParams, SchemeRegistry, SSLSocketFactory, NameValuePair, HttpResponse are deprecated.
我尝试使用HttpsUrlConnection,但对它们感到困惑.
I tried to use HttpsUrlConnection but i confused about them.
protected Gson gson;
private ThreadSafeClientConnManager threadSafeClientConnManager;
private DefaultHttpClient client;
AbstractServiceApi() {
// sets up parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, ENCODING);
HttpConnectionParams.setConnectionTimeout(params, 95 * 1000);
HttpConnectionParams.setSoTimeout(params, 95 * 1000);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
params.setBooleanParameter("http.protocol.expect-continue", false);
// registers schemes for both http and https
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));
threadSafeClientConnManager = new ThreadSafeClientConnManager(params, registry);
client = new DefaultHttpClient(threadSafeClientConnManager, params);
gson = new Gson();
}
我没有密钥库. https://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html
KeyStore keyStore = ...;
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
URL url = new URL("https://www.example.com/");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
有人可以帮助我吗?
推荐答案
这是我的解决方案.
private HttpsURLConnection urlConnection;
private CookieManager cookieManager;
private HttpsURLConnection getConnection(String url) throws MalformedURLException {
URL request_url = new URL(url);
try {
if (!isHttps()) {
throw new ConnectException("you have to use SSL certifacated url!");
}
urlConnection = (HttpsURLConnection) request_url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setReadTimeout(95 * 1000);
urlConnection.setConnectTimeout(95 * 1000);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("X-Environment", "android");
/** Cookie Sets... */
String cookie = cookieManager.getCookie(urlConnection.getURL().toString());
cookieManager = CookieManager.getInstance();
if (cookie != null)
urlConnection.setRequestProperty("Cookie", cookie);
List<String> cookieList = urlConnection.getHeaderFields().get("Set-Cookie");
if (cookieList != null) {
for (String cookieTemp : cookieList) {
cookieManager.setCookie(urlConnection.getURL().toString(), cookieTemp);
}
}
/** Cookie Sets... */
urlConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
/** if it necessarry get url verfication */
//return HttpsURLConnection.getDefaultHostnameVerifier().verify("your_domain.com", session);
return true;
}
});
urlConnection.setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
urlConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
return urlConnection;
}
这篇关于如何使用HttpsUrlConnection代替DefaultHttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文