AndroidHttpClient USES Cookie application analysis

  • 2020-04-01 01:08:37
  • OfStack

Today, I want to port a small automatic check-in program using HttpClient to Android. Fortunately, the Android SDK comes with the HttpClient package. When looking through the Android documents, I found that the government also provided an AndroidHttpClient which realized the HttpClient interface. I searched the Internet and found no articles about AndroidHttpClient. Of course, you can still use DefaultHttpClient, but AndroidHttpClient is better for Android.
Here are the two test httpservlets :
 
public class LogIn extends HttpServlet { 
/** 
* Processes requests for both HTTP 
* <code>GET</code> and 
* <code>POST</code> methods. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
response.setContentType("text/html;charset=UTF-8"); 
request.setCharacterEncoding("utf-8"); 
PrintWriter out = response.getWriter(); 
HttpSession session=request.getSession(); 
String info=request.getParameter("info"); 
session.setAttribute("info", info); 
try { 
 
out.println("OK"); 
} finally { 
out.close(); 
} 
} 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP 
* <code>GET</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
processRequest(request, response); 
} 
/** 
* Handles the HTTP 
* <code>POST</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
processRequest(request, response); 
} 
 
@Override 
public String getServletInfo() { 
return "Short description"; 
}// </editor-fold> 
} 

 
public class Info extends HttpServlet { 
/** 
* Processes requests for both HTTP 
* <code>GET</code> and 
* <code>POST</code> methods. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
response.setContentType("text/html;charset=UTF-8"); 
PrintWriter out = response.getWriter(); 
HttpSession session=request.getSession(); 
String info=(String)session.getAttribute("info"); 
try { 
 
if(info==null) 
out.print("null"); 
else 
out.print(info); 
} finally { 
out.close(); 
} 
} 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP 
* <code>GET</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
processRequest(request, response); 
} 
/** 
* Handles the HTTP 
* <code>POST</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
processRequest(request, response); 
} 
 
@Override 
public String getServletInfo() { 
return "Short description"; 
}// </editor-fold> 
} 

The main code is in processRequest, and you don't need to look at the rest.
Pass a value with the name info when accessing LogIn, and the browser gets a cookie to locate the server session. And then you go to Info, and if you have a cookie the server can find the value that you just passed and return it to you, but if you don't have a cookie it can't be found.
Android code:
 
public class MainActivity extends Activity { 
private AndroidHttpClient mHttpclient=AndroidHttpClient.newInstance(""); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
findViewById(R.id.button1).setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
new Thread(rTest).start(); 
} 
}); 
} 
private String toString(InputStream is) throws IOException{ 
String ret=""; 
InputStreamReader isr=new InputStreamReader(is); 
BufferedReader br=new BufferedReader(isr); 
String tmp=br.readLine(); 
while(tmp!=null){ 
ret+=tmp; 
tmp=br.readLine(); 
} 
br.close(); 
return ret; 
} 
private Runnable rTest=new Runnable() { 
@Override 
public void run() { 
// TODO Auto-generated method stub 
try { 
BasicHttpContext context=new BasicHttpContext(); 
context.setAttribute(ClientContext.COOKIE_STORE,new BasicCookieStore()); 
HttpPost httppost = new HttpPost("http://10.226.233.48:8080/WebApplication1/LogIn"); 
List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
nvps.add(new BasicNameValuePair("info", " hello   The world!!!!! ")); 
httppost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
HttpResponse response=mHttpclient.execute(httppost,context); 
HttpEntity entity = response.getEntity(); 
Log.i("kagami", MainActivity.this.toString(entity.getContent())); 
entity.consumeContent(); 
HttpGet httpget2 = new HttpGet("http://10.226.233.48:8080/WebApplication1/Info"); 
HttpResponse response2=mHttpclient.execute(httpget2,context); 
HttpEntity entity2 = response2.getEntity(); 
Log.i("kagami", MainActivity.this.toString(entity2.getContent())); 
entity2.consumeContent(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
}; 
 }  

(link: http://images.cnblogs.com/cnblogs_com/kagami/201211/201211262016095739.png)  
The difference between AndroidHttpClient and DefaultHttpClient :
AndroidHttpClient cannot execute on the main thread and will throw an exception. AndroidHttpClient obtains the instance through the static method newInstance, and the parameter is the proxy. DefaultHttpClient is Cookie enabled by default, AndroidHttpClient is not Cookie enabled by default, if you want to use it, you should add an HttpContext parameter every time execute, and add CookieStore. Don't forget to close when you're done or you won't be able to create a new instance.

Related articles: