hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ananth Vasudevan <ananth.vasude...@gmail.com>
Subject Question about using SSL in HttpClient 4.1.2
Date Tue, 17 Jan 2012 04:01:42 GMT
Hi,

I'm migrating from HttpClient 3.x to HttpClient 4.1.2. I'm using http core
4.1.4.
The HttpClient 3.x code posts some data to a SSL enabled URL. We've to
support IPv6 literals and I'm trying to follow the samples and getting an
exception as below:
java.security.AccessControlException: access denied
(java.net.SocketPermission [fd07:2fa:6cff:2021:221:9bff:fe97:d061]:443
connect,resolve)

When I run it from eclipse as an application, it runs and gives the
response. But I'm trying to run it as an applet. The applet is signed using
a verisign
code signing certificate. Ay Pointers/Help is greatly appreciated.

Here's the code snippet:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.NameValuePair;

public class HttpClientUtil {

HttpClient httpClient;
private static final int HTTP_RESPONSE_TIMEOUT = 60*1000;
SSLContext context = null;

public void HttpClientUtil(){
doHttpPost();
}
 private String doHttpPost(){

TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    return null;
}
public void checkClientTrusted(
    java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
    java.security.cert.X509Certificate[] certs, String authType) {
}
    }
};

try {
context = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
try {
context.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e1) {
e1.printStackTrace();
}
SSLSocketFactory sf = new SSLSocketFactory(context,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 443, sf);

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
HTTP_RESPONSE_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, HTTP_RESPONSE_TIMEOUT);

ClientConnectionManager ccm = new
ThreadSafeClientConnManager();//httpParams);
ccm.getSchemeRegistry().register(https);

httpClient = new DefaultHttpClient(ccm, httpParams);
HttpPost postMethod;
 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
String host = "https://[fd07:2fa:6cff:2021:221:9bff:fe97:d061]";
 postMethod = new HttpPost(host);
try{
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}catch(UnsupportedEncodingException uex){
uex.printStackTrace();
}
StringBuffer responseStr = null;
try {
HttpResponse response = httpClient.execute(postMethod);
int statusCode = -1;
statusCode = response.getStatusLine().getStatusCode();
responseStr = new StringBuffer();

BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
responseStr.append(line);
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(IllegalArgumentException iae){
iae.printStackTrace();
}

return new String(responseStr);
}
 public static void main(String a[]){
HttpClientUtil util = new HttpClientUtil();
String response = util.doHttpPost();
System.out.println(response);
 }

}

Best Regards,
Ananth Vasudevan

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message