-
-
Save IRMobydick/f770f724d14c13ca428acf398be56b21 to your computer and use it in GitHub Desktop.
An Android activity that will demonstrate basic authentication mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment