Skip to content

Instantly share code, notes, and snippets.

@rajiv-singaseni
Created August 1, 2011 09:32

Revisions

  1. rajiv-singaseni created this gist Aug 1, 2011.
    55 changes: 55 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    package com.webile.basicauth;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;

    import android.app.Activity;
    import android.os.Bundle;

    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    basicAuthDemo();
    }

    private static final String HOST_NAME = <Your host name>;
    private static final String URL = <Your url here>;
    private static final String USER_NAME = <User name>;
    private static final String PASSWORD = <Password>;
    private void basicAuthDemo() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
    httpclient.getCredentialsProvider().setCredentials(
    new AuthScope(HOST_NAME, 443),
    new UsernamePasswordCredentials(USER_NAME, PASSWORD));

    HttpGet httpget = new HttpGet(URL);

    System.out.println("executing request" + httpget.getRequestLine());
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
    System.out.println("Response content length: " + entity.getContentLength());
    System.out.println(EntityUtils.toString(entity));
    }
    } catch(Exception e){
    e.printStackTrace();
    }finally {
    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
    }
    }
    }