The following Android app needs to send a request to the specified page, but the page is not a simple page and can only be accessed if the user is logged in and the user name of the logged-on user is crazyit.org. If you use HttpURLConnection to access the page, the details you need to deal with are too complex.
Access to the Web application protected pages, if the use of the browser is very simple, the user through the system provided by the login page login system, the browser will be responsible for maintaining the session between the server, if the user login user name, password meet the requirements, you can access the protected resources.
In order to access the protected page through HttpClient, the program also needs to use HttpClient to log on to the system, as long as the application uses the same httpclient send the request, HttpClient will automatically maintain the session state with the server, This means that the first time the program uses HttpClient login system, the next use httpclient can access the protected page.
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;
import org.apache.http.HttpEntity;
import Org.apache.http.HttpResponse;
import Org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import Org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import Org.apache.http.message.BasicNameValuePair;
import Org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
Public class Httpclienttest extends Activity {
Button get;
Button login;
EditText response;
HttpClient HttpClient;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_http_client_test);
//Create Defaulthttpclient Object
httpClient = new Defaulthttpclient ();
get = (Button) Findviewbyid (r.id.get);
login = (Button) Findviewbyid (r.id.login);
response = (EditText) Findviewbyid (r.id.response);
Get.setonclicklistener (New Onclicklistener () {
@Override
Public void OnClick (View v) {
//Create a HttpGet object
HttpGet get = new HttpGet ("http://172.18.5.198:8080/foo/secret.jsp");
try {
//Send GET request
HttpResponse HttpResponse = Httpclient.execute (get);
httpentity entity = httpresponse.getentity ();
if (entity! = null) {
//Read server response
bufferedreader br = new BufferedReader (
New InputStreamReader (Entity.getcontent ()));
String line = null;
While (line = Br.readline ()) = null) {
//Use the Response text box to display the server response
Response.append (line + "\ n");
}
}
} catch (Exception e) {
e.printstacktrace ();
}
}
});
Login.setonclicklistener (New Onclicklistener () {
@Override
Public void OnClick (View v) {
final View logindialog = Getlayoutinflater ()
. Inflate (r.layout.login, null);
new Alertdialog.builder (httpclienttest.this)
. Settitle ("Login System")
. Setview (Logindialog)
. Setpositivebutton ("Login", new Dialoginterface.onclicklistener () {
@Override
Public void OnClick (dialoginterface dialog, int which) {
String name = ((EditText) Logindialog.findviewbyid (
r.id.username)). GetText (). toString ();
String pass = ((EditText) Logindialog.findviewbyid (
R.id.password)). GetText (). toString ();
HttpPost post = new HttpPost ("http://172.18.5.198:8080/foo/login.jsp");
//If the number of passed parameters is more than the passed parameters can be encapsulated
list<namevaluepair> params = new arraylist<namevaluepair> ();
Params.add (New Basicnamevaluepair ("name", name));
Params.add (New Basicnamevaluepair ("Pass", pass));
try {
//Set Request parameters
post.setentity (New Urlencodedformentity (params,http. Utf_8));
//Send POST request
HttpResponse response = Httpclient.execute (post);
//If the server succeeds in returning a response
if (Response.getstatusline (). Getstatuscode () = = () {
String msg = entityutils.tostring (response.getentity ());
//Prompt Login Success
Toast.maketext (Httpclienttest.this, MSG,). Show ();
}
} catch (Exception e) {
e.printstacktrace ();
}
}
}). Setnegativebutton ("Cancel", null). Show ();
}
});
}
}
Access protected resources using HttpClient