Recently need to implement a function, is the need to send SMS to register, now want to put the SMS verification into the server session value, When the client receives the text message and submits the SMS code, it is judged by the ASP. Then how to share the session requires a few lines of code to be added to the Android client. Do the following. The first time the data request gets the name of the cookie and gets the value of the cookie, which is the value of SessionID and is stored in a static variable, and then the second time the data is requested to put this sessionid in a cookie sent to the server, The server is through this SessionId to identify exactly the client in the request data, in ASP. This SessionId name is called Asp.net_sessionid, of course, we can get from the program, the following code:
Get the name of this sessionid on the service side
/* Get Cookiestore */
list<cookie> cookies = cookiestore.getcookies ();
for (int i=0;i<cookies.size (); i++) {
String SessionID = Cookies.get (i). GetName (); From here you can get to this sessionid, which is Asp.net_sessionid
}
Gets the value of the SessionID
for (int i=0;i<cookies.size (); i++) {
if ("Asp.net_sessionid". Equals (Cookies.get (i). GetName ())) {
Jsessionid = Cookies.get (i). GetValue (); This is the value of SessionID.
Break
}
}
The complete httputils code is as follows:
public static defaulthttpclient httpClient = null;
private static String jsessionid;//define a static field, save SessionID
public static string getrequest (string url)
{
HttpClient = new Defaulthttpclient ();
HttpGet get = new HttpGet (URL);
Try
{
Httpparams params = new Basichttpparams ();
Httpconnectionparams.setconnectiontimeout (params, 10000);//Set Connection timeout
Httpconnectionparams.setsotimeout (params, 15000);//Set Request timed out
Get.setparams (params);
Get.setheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
if (null! = Jsessionid) {
Get.setheader ("Cookie", "asp.net_sessionid=" +jsessionid);
}
Connection response, obtaining response information via HttpResponse
HttpResponse HttpResponse = Httpclient.execute (get);
if (Httpresponse.getstatusline (). Getstatuscode () = = 200)
{
Get response string
String result = entityutils.tostring (httpresponse.getentity ());
return result;
}
}
catch (Clientprotocolexception e)
{
return null;
}
catch (IOException E)
{
return null;
}
return null;
}
public static string postrequest (string url, hashmap<string, string> rawparams) throws Exception
{
HttpClient = new Defaulthttpclient ();
How to create a POST request
HttpPost post = new HttpPost (URL);
Httpparams cparams = new Basichttpparams ();
Httpconnectionparams.setconnectiontimeout (Cparams, 10000);//Set Connection timeout
Httpconnectionparams.setsotimeout (Cparams, 15000);//Set Request timed out
Post.setparams (Cparams);
Post.setheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
if (null! = Jsessionid) {
Post.setheader ("Cookie", "asp.net_sessionid=" +jsessionid);
}
Using Namevaluepair to save the arguments to be passed, you can use Basicnamevaluepair to construct a parameter to be passed
Add this parameter to Namevaluepair via add
arraylist<namevaluepair> params = new arraylist<namevaluepair> ();
For (String Key:rawParams.keySet ())
{
Add a parameter to pass
Params.add (New Basicnamevaluepair (Key, Rawparams.get (key)));
}
Post requires setting the character set for the parameter
Httpentity httpentity = new Urlencodedformentity (params, HTTP. UTF_8);
Request HttpRequest
Post.setentity (httpentity);
Send a POST request and get a response
HttpResponse HttpResponse = null;
Try
{
HttpResponse = Httpclient.execute (POST);
}
catch (Exception ex)
{
String ee = ex.getmessage ();
}
if (Httpresponse.getstatusline (). Getstatuscode () = = 200)
{
String result = entityutils.tostring (Httpresponse.getentity (), HTTP. UTF_8);
/ * Get Cookiestore
Asp.net_sessionid is obtained by means of the above method.
*/
Cookiestore Cookiestore = Httpclient.getcookiestore ();
list<cookie> cookies = cookiestore.getcookies ();
for (int i=0;i<cookies.size (); i++) {
if ("Asp.net_sessionid". Equals (Cookies.get (i). GetName ())) {
Jsessionid = Cookies.get (i). GetValue ();
Break
}
}
return result;
}
return null;
}
How Android shares the session with the ASP