最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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默認(rèn)是啟用Cookie的,AndroidHttpClient默認(rèn)不啟用Cookie,要使用的話每次execute時要加一個HttpContext參數(shù),并且添加CookieStore。用完后別忘了close不然不能創(chuàng)建新實例。

相關(guān)文章

  • SPRINGMVC 406問題解決方案

    SPRINGMVC 406問題解決方案

    這篇文章主要介紹了SPRINGMVC 406問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • JDK8中的HashMap初始化和擴容機制詳解

    JDK8中的HashMap初始化和擴容機制詳解

    這篇文章主要介紹了JDK8中的HashMap初始化和擴容機制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中的Kafka為什么性能這么快及4大核心詳析

    Java中的Kafka為什么性能這么快及4大核心詳析

    這篇文章主要介紹了Java中的Kafka為什么性能這么快及4大核心詳析,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Java面試Socket編程常用參數(shù)設(shè)置源碼問題分析

    Java面試Socket編程常用參數(shù)設(shè)置源碼問題分析

    這篇文章主要為大家介紹了Java編程中關(guān)于Socket結(jié)構(gòu)分析,常用參數(shù)設(shè)置源碼示例以及面試中的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • Java實現(xiàn)英文猜詞游戲的示例代碼

    Java實現(xiàn)英文猜詞游戲的示例代碼

    這篇文章主要介紹了如何用Java編寫一個英文猜詞游戲,可以用來背英語單詞。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-02-02
  • SpringBoot集成SFTP客戶端實現(xiàn)文件上傳下載實例

    SpringBoot集成SFTP客戶端實現(xiàn)文件上傳下載實例

    這篇文章主要為大家介紹了SpringBoot集成SFTP客戶端實現(xiàn)文件上傳下載實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java的枚舉類型使用方法詳解

    Java的枚舉類型使用方法詳解

    這篇文章主要介紹了Java的枚舉類型使用方法詳解,從背景、到定義、特點、使用方式做個簡單了解,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Java 覆蓋equals時總要覆蓋hashcode

    Java 覆蓋equals時總要覆蓋hashcode

    這篇文章主要介紹了Java 覆蓋equals時總要覆蓋hashcode的相關(guān)資料,這里附有實例代碼,具有參考價值,需要的朋友可以參考下
    2016-12-12
  • java中Map如何根據(jù)key的大小進行排序詳解

    java中Map如何根據(jù)key的大小進行排序詳解

    這篇文章主要給大家介紹了關(guān)于java中Map如何根據(jù)key的大小進行排序的相關(guān)資料,有時候我們業(yè)務(wù)上需要對map里面的值按照key的大小來進行排序的時候我們就可以利用如下方法來進行排序了,需要的朋友可以參考下
    2023-09-09
  • SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例

    SpringBoot中添加監(jiān)聽器及創(chuàng)建線程的代碼示例

    這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽器及創(chuàng)建線程,文中有詳細(xì)的代碼示例,具有一定的參考價值,需要的朋友可以參考下
    2023-06-06

最新評論

奎屯市| 安溪县| 昭通市| 大埔区| 洛扎县| 天长市| 乃东县| 铜鼓县| 天门市| 金堂县| 新乡市| 许昌市| 蒙城县| 玉环县| 葫芦岛市| 宜黄县| 澎湖县| 嘉黎县| 长汀县| 阿拉尔市| 庐江县| 衢州市| 西平县| 永顺县| 清徐县| 威信县| 城固县| 大丰市| 盐池县| 湘潭市| 涟源市| 若尔盖县| 威宁| 班玛县| 江山市| 湟源县| 白山市| 西畴县| 始兴县| 克拉玛依市| 洛阳市|