欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AndroidHttpClient使用Cookie應(yīng)用分析

 更新時間:2012年11月27日 11:13:31   作者:  
今天想把一個用使用了HttpClient的自動簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包.當(dāng)然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好
今天想把一個用使用了HttpClient的自動簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包。翻Android的文檔時發(fā)現(xiàn)官方還提供了一個實現(xiàn)了HttpClient接口的AndroidHttpClient,上網(wǎng)搜了下沒發(fā)現(xiàn)關(guān)于AndroidHttpClient的文章。當(dāng)然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好。
下面是2個測試用的HttpServlet
復(fù)制代碼 代碼如下:

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 {
/* TODO output your page here. You may use following sample code. */
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);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

復(fù)制代碼 代碼如下:

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 {
/* TODO output your page here. You may use following sample code. */
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);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

主要代碼在processRequest里,其他可以不用看。
訪問LogIn時傳一個name為info的值,這時瀏覽器會得到一個用于定位服務(wù)端session的cookie。然后訪問Info,如果有cookie的話服務(wù)端能找到剛才你傳的值并返回給你,沒帶cookie的話就不能找到。
Android端代碼:
復(fù)制代碼 代碼如下:

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", "你好 世界!!"));
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();
}
}
};


捕獲 
AndroidHttpClient和DefaultHttpClient的區(qū)別
AndroidHttpClient不能在主線程中execute,會拋出異常。AndroidHttpClient通過靜態(tài)方法newInstance獲得實例,參數(shù)是代理,不用代理的話填“”。DefaultHttpClient默認是啟用Cookie的,AndroidHttpClient默認不啟用Cookie,要使用的話每次execute時要加一個HttpContext參數(shù),并且添加CookieStore。用完后別忘了close不然不能創(chuàng)建新實例。

相關(guān)文章

  • 詳解Java中Period類的使用方法

    詳解Java中Period類的使用方法

    Period類通過年、月、日相結(jié)合來描述一個時間量,最高精度是天。本文將通過示例詳細為大家講講Period類的使用,需要的可以參考一下
    2022-05-05
  • SpringBoot實現(xiàn)異步消息處理的代碼示例

    SpringBoot實現(xiàn)異步消息處理的代碼示例

    在現(xiàn)代應(yīng)用程序中,異步消息處理是一項至關(guān)重要的任務(wù)。它可以提高應(yīng)用程序的性能、可伸縮性和可靠性,同時也可以提供更好的用戶體驗,本文將介紹如何使用Spring Boot實現(xiàn)異步消息處理,并提供相應(yīng)的代碼示例
    2023-06-06
  • Java序列化機制詳解

    Java序列化機制詳解

    Java 序列化機制是一種將對象轉(zhuǎn)換為字節(jié)流的過程,以便在網(wǎng)絡(luò)上傳輸或保存到文件中,并能在需要時將字節(jié)流還原為對象,這一機制通過實現(xiàn) java.io.Serializable 接口來實現(xiàn),同時涉及到一些關(guān)鍵概念和注意事項,需要的朋友可以參考下
    2023-12-12
  • IntelliJ IDEA2023中運行Spring Boot找不到VM options進行端口的修改的問題解決

    IntelliJ IDEA2023中運行Spring Boot找不到VM options進

    這篇文章主要介紹了IntelliJ IDEA2023中運行Spring Boot找不到VM options進行端口的修改的問題解決,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • 詳解Spring如何解析占位符

    詳解Spring如何解析占位符

    Spring一直支持將屬性定義到外部的屬性的文件中,并使用占占位符的形式為使用"${}"包裝的屬性名稱,為了使用屬性占位符,我們必須配置一個PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer實例,本文將介紹如何解析占位符
    2021-06-06
  • Java中的時間日期API知識點總結(jié)

    Java中的時間日期API知識點總結(jié)

    本文給大家總結(jié)了Java中的時間日期API知識點以及相關(guān)的實例代碼分享,有興趣的朋友參考學(xué)習(xí)下。
    2018-04-04
  • Java讀取Excel文件內(nèi)容的簡單實例

    Java讀取Excel文件內(nèi)容的簡單實例

    這篇文章主要介紹了Java讀取Excel文件內(nèi)容的簡單實例,有需要的朋友可以參考一下
    2013-11-11
  • java GUI編程之布局控制器(Layout)實例分析

    java GUI編程之布局控制器(Layout)實例分析

    這篇文章主要介紹了java GUI編程之布局控制器(Layout),結(jié)合實例形式分析了java GUI編程中布局控制器(Layout)具體功能、用法及相關(guān)操作注意事項,需要的朋友可以參考下
    2020-01-01
  • java String類常量池分析及

    java String類常量池分析及"equals"和"==”區(qū)別詳細介紹

    這篇文章主要介紹了java String類常量池分析及"equals"和"==”區(qū)別詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • 詳解springboot中junit回滾

    詳解springboot中junit回滾

    本篇文章主要介紹了springboot中junit回滾,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論