Hello Sang,
Hope you are doing good. I am your student at jpassion.com. I need some
help and I was wondering if you can help me and provide me some suggestions
for the below problem.
*I have org.apache Classes used for networking in my app. As it is
deprecated now i want to use HttpUrlConnection and no 3rd party library.
Main challenge i am facing is with ThreadLocal. I have a restclient which
is using DefaultHttpClient and want to replace it with HttpUrlConnection as
shown in below code*
public static class UniqueHttpClient {
private static ThreadLocal<DefaultHttpClient> uniquelocal =
new ThreadLocal<DefaultHttpClient>() {
@Override
protected DefaultHttpClient initialValue() {
BasicHttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams
.setConnectionTimeout(httpParameters, 90000);
HttpConnectionParams.setSoTimeout(httpParameters, 90000);
DefaultHttpClient client;
// registers schemes for both http and httpsb
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
KeyStore trustStore;
SSLSocketFactory sslSocketFactory;
try {
trustStore = KeyStore
.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
sslSocketFactory = new MySSLSocketFactory(trustStore);
} catch (Exception exEXCP) {
exEXCP.printStackTrace();
sslSocketFactory = SSLSocketFactory.getSocketFactory();
}
sslSocketFactory
.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));
ThreadSafeClientConnManager manager = new
ThreadSafeClientConnManager(
httpParameters, registry);
client = new DefaultHttpClient(manager, httpParameters);
return client;
}
};
public static DefaultHttpClient getUniqueHttpClient() {
return uniquelocal.get();
}
} // class UniqueHttpClient
*If i try to replace DefaultHttpClient with HttpUrlConnection, how can i
get the Url here to open the connection?*
*I have seen various posts related to ThreadLocal but i am not able to
understand how i can pass url and return the client instance.*
*To clarify more i want to do something like this..*
public static class UniqueHttpClient {
private static ThreadLocal<HttpURLConnection> uniquelocal =
new ThreadLocal<HttpURLConnection >() {
@Override
protected HttpURLConnection initialValue() {
HttpURLConnection urlConn = null;
try {
URL requestUrl = new URL(url);
urlConn = (HttpURLConnection)
requestUrl.openConnection();
urlConn.setConnectTimeout(90000);
urlConn.setReadTimeout(90000);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// registers schemes for both http and httpsb
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
KeyStore trustStore;
SSLSocketFactory sslSocketFactory;
try {
trustStore = KeyStore
.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
sslSocketFactory = new
MySSLSocketFactory(trustStore);
} catch (Exception exEXCP) {
exEXCP.printStackTrace();
sslSocketFactory =
SSLSocketFactory.getSocketFactory();
}
sslSocketFactory
.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory,
443));
//client = new DefaultHttpClient(manager,
httpParameters);
return urlConn;
}
};
*In the above code how i can get the url(requestUrl) from multiple clients
so that i would be able to openConnection successfully and return the
urlConn object. *
*If you can provide me some code example that will be really helpful. *
*Your help will be really appreciated. *
Thanks & Regards,
Puja
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.