Accessing secure wiki pages in Java

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.auth.NegotiateSchemeFactory;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
public class Test {
  * @param args
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 try {
            String pageSource = "";
            HttpHost targetHost = new HttpHost("");
            DefaultHttpClient httpclient = new DefaultHttpClient();
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials("<yourUsername>","<yourPassword>");
            httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), creds);
            NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
            httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(targetHost, basicAuth);
            // Add AuthCache to the execution context
            BasicHttpContext localcontext = new BasicHttpContext();
            localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);      
            httpclient = wrapClient(httpclient);
            HttpGet get = new HttpGet("<yourWikiPage>");
            get.addHeader(new BasicScheme().authenticate(creds,get));
            HttpResponse response = httpclient.execute(targetHost,get,localcontext);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
     pageSource = pageSource + line +"\n";
     catch (Exception e) {
 public static DefaultHttpClient wrapClient(HttpClient base) { 
 try { 
  SSLContext ctx = SSLContext.getInstance("TLS"); 
  X509TrustManager tm = 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; 
  ctx.init(null, new TrustManager[]{tm}, null); 
  SSLSocketFactory ssf = new SSLSocketFactory(ctx); 
  ClientConnectionManager ccm = base.getConnectionManager(); 
  SchemeRegistry sr = ccm.getSchemeRegistry(); 
  sr.register(new Scheme("https", ssf, 443)); 
  return new DefaultHttpClient(ccm, base.getParams()); 
  } catch (Exception ex) { 
   return null; 
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s