Reprinted from http://dai-lm.iteye.com/blog/1160191
1. Create a connection configuration class
- Class useragentconfig {
- Public String host;
- Public String scheme = "HTTP ";
- Public int Port = 80;
- Public int timeoutconnection = 3000;
- Public int timeoutsocket = 20000;
- Public String username = "";
- Public String Password = "";
- }
2. encapsulate the request class
- Public class httprequest {
- /**
- * Get "stream" as response only based on URL
- *
- * @ Param strurl
- * @ Return
- */
- Public static inputstream getstream (string strurl ){
- Inputstream input = NULL;
- Try {
- URL url = new URL (strurl );
- Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
- Input = urlcon. getinputstream ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- Return input;
- }
- /**
- * Get "stream" as response
- *
- * @ Param URL request URL
- * @ Param config connection config
- * @ Param Params parameter for post method
- * @ Return response in stream
- * @ Throws ioexception
- * @ Throws illegalstateexception
- */
- Public static inputstream getstream (string URL, useragentconfig config,
- List <basicnamevaluepair> Params ){
- Httpentity entity = httprequest (URL, config, Params );
- Inputstream ret = NULL;
- If (entity! = NULL ){
- Try {
- Byte [] B = entityutils. tobytearray (entity );
- Ret = new bytearrayinputstream (B );
- } Catch (exception e ){
- E. printstacktrace ();
- } Finally {
- Release (entity );
- }
- }
- Return ret;
- }
- /**
- * Get "string" as response
- *
- * @ Param URL
- * @ Param config
- * @ Param Params
- * @ Return
- */
- Public static string getstring (string URL, useragentconfig config,
- List <basicnamevaluepair> Params ){
- Httpentity entity = httprequest (URL, config, Params );
- String ret = NULL;
- If (entity! = NULL ){
- Try {
- Ret = entityutils. tostring (entity );
- } Catch (exception e ){
- E. printstacktrace ();
- } Finally {
- Release (entity );
- }
- }
- Return ret;
- }
- Private Static void release (httpentity entity ){
- Try {
- Entity. consumecontent ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- /**
- * Get "httpentity" as response
- *
- * @ Param URL request URL
- * @ Param config connection config
- * @ Param Params parameter for post method
- * @ Return
- */
- Private Static httpentity httprequest (string URL, useragentconfig config,
- List <basicnamevaluepair> Params ){
- Defaulthttpclient client = NULL;
- Httpentity entity = NULL;
- Try {
- Basichttpparams httpparameters = new basichttpparams ();
- // Set connection timeout
- Httpconnectionparams. setconnectiontimeout (httpparameters,
- Config. timeoutconnection );
- Httpconnectionparams. setsotimeout (httpparameters,
- Config. timeoutsocket );
- Client = new defaulthttpclient (httpparameters );
- // Retry 3 times
- Defaulthttprequestretryhandler retryhandler = new defaulthttprequestretryhandler (3, true );
- Client. sethttprequestretryhandler (retryhandler );
- // Set username & password if available
- If (! ("". Equals (config. username) & "". Equals (config. Password ))){
- Authscope as = new authscope (config. Host, config. Port );
- Usernamepasswordcredentials UPC = new usernamepasswordcredentials (
- Config. username, config. Password );
- Client. getcredentialsprovider (). setcredentials (AS, UPC );
- }
- Basichttpcontext localcontext = new basichttpcontext ();
- Basicscheme basicauth = new basicscheme ();
- Localcontext. setattribute ("preemptive-auth", basicauth );
- Httphost targethost = new httphost (config. Host, config. Port,
- Config. scheme );
- // Check get or POST method by Params
- Httprequestbase method = NULL;
- If (Params = NULL ){
- Method = new httpget (URL );
- } Else {
- Method = new httppost (URL );
- (Httppost) method). setentity (New urlencodedformentity (Params,
- "UTF-8 "));
- }
- Method. setheader ("Content-Type", "application/XML ");
- Httpresponse response = client.exe cute (targethost, method,
- Localcontext );
- Entity = response. getentity ();
- } Catch (exception e ){
- E. printstacktrace ();
- }
- Return entity;
- }
- }
3. Call
GET request
- String url = "...";
- Useragentconfig Config = new useragentconfig ();
- Config. Host = "...";
- Test (URL, config, null );
POST request
- String url = "...";
- Useragentconfig Config = new useragentconfig ();
- Config. Host = "...";
- Config. Username = "...";
- Config. Password = "...";
- Arraylist <basicnamevaluepair> Params = new arraylist <basicnamevaluepair> ();
- Params. Add (New basicnamevaluepair ("...","..."));
- ...
- Test (URL, config, Params );