Package com.rainet.tiis.network;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.NameValuePair;
Import Org.apache.http.client.CookieStore;
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.client.params.HttpClientParams;
Import Org.apache.http.cookie.Cookie;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.params.BasicHttpParams;
Import Org.apache.http.params.HttpConnectionParams;
Import Org.apache.http.params.HttpParams;
Import Org.apache.http.params.HttpProtocolParams;
Import Org.apache.http.protocol.HTTP;
Import Org.apache.http.util.EntityUtils;
Import Android.util.Log;
/**
* @project: Tiis
* @Title: Simpleclient.java
* @Package Com.rainet.tiis.network
* @Description: HTTP request factory
* @author Yang Guisong
* @date 2014-3-18 12:39:23
* @version V1.0
*/
public class SimpleClient {
private static Httpparams Httpparams;
private static defaulthttpclient httpClient;
private static String Jsessionid; Define a static field, save SessionID
/**
* @Title: Gethttpclient
* @author Yang Guisong
* @date 2014-3-18 1:11:18
* @Description: Get httpclient
* @return
* @throws Exception
* HttpClient return
*/
public static HttpClient Gethttpclient () throws Exception {
Create Httpparams to use to set HTTP parameters (this part is not required)
Httpparams = new Basichttpparams ();
Set connection timeout and socket timeout, and socket cache size
Httpconnectionparams.setconnectiontimeout (Httpparams, 20 * 1000);
Httpconnectionparams.setsotimeout (Httpparams, 20 * 1000);
Httpconnectionparams.setsocketbuffersize (Httpparams, 8192);
Set redirection, default to True
Httpclientparams.setredirecting (Httpparams, true);
Setting up the user agent
String useragent = "mozilla/5.0 (Windows; U Windows NT 5.1; ZH-CN; rv:1.9.2) gecko/20100115 firefox/3.6 ";
Httpprotocolparams.setuseragent (Httpparams, useragent);
Create a HttpClient instance
Note HttpClient HttpClient = new HttpClient (); It's Commons HttpClient.
In the usage of Android 1.5 we need to use Apache's default implementation defaulthttpclient
HttpClient = new Defaulthttpclient (httpparams);
return httpClient;
}
/**
* @Title: Doget
* @author Yang Guisong
* @date 2014-3-18 12:39:58
* @Description: Doget Request
* @param URL
* @param params
* @return
* @throws Exception
* String returns
*/
@SuppressWarnings ("Rawtypes")
public static string doget (string url, Map params) throws Exception {
/* Build HttpGet Object */
String paramstr = "";
if (params! = null) {
Iterator iter = Params.entryset (). Iterator ();
while (Iter.hasnext ()) {
Map.entry Entry = (map.entry) iter.next ();
Object key = Entry.getkey ();
Object val = Entry.getvalue ();
Paramstr + = Paramstr = "&" + key + "=" + val;
}
}
if (!paramstr.equals ("")) {
Paramstr = Paramstr.replacefirst ("&", "?");
URL + = paramstr;
}
HttpGet HttpRequest = new HttpGet (URL);
String strresult = "Dogeterror";
/* Send request and wait for response */
HttpResponse HttpResponse = Httpclient.execute (HttpRequest);
/* If the status code is OK */
if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {
/* Read back Data */
strresult = entityutils.tostring (Httpresponse.getentity ());
} else {
strresult = "Error Response:" + httpresponse.getstatusline (). toString ();
}
LOG.V ("Strresult", strresult);
return strresult;
}
/**
* @Title: DoPost
* @author Yang Guisong
* @date 2014-3-18 12:39:38
* @Description: Dopost Request
* @param URL
* @param params
* @return
* @throws Exception
* String returns
*/
public static string DoPost (string url, list<namevaluepair> params) throws Exception {
/* Build HttpPost Object */
HttpPost HttpRequest = new HttpPost (URL);
String strresult = "Doposterror";
/* Add request parameter to request object */
if (params! = null && params.size () > 0) {
Httprequest.setentity (New urlencodedformentity (params, HTTP. Utf_8));
}
if (null! = Jsessionid) {
Httprequest.setheader ("Cookie", "jsessionid=" +jsessionid);
}
/* Send request and wait for response */
HttpResponse HttpResponse = Httpclient.execute (HttpRequest);
/* If the status code is OK */
if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {
/* Read back Data */
strresult = entityutils.tostring (Httpresponse.getentity ());
/* Get Cookiestore */
Cookiestore Cookiestore = Httpclient.getcookiestore ();
list<cookie> cookies = cookiestore.getcookies ();
for (int i=0;i<cookies.size (); i++) {
if ("Jsessionid". Equals (Cookies.get (i). GetName ())) {
Jsessionid = Cookies.get (i). GetValue ();
Break
}
}
}
LOG.V ("Strresult", strresult);
return strresult;
}
}
Android requests the Web server via httpclient and resolves the user login session to remain