麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

AndroidHttpClient使用Cookie應用分析

2019-11-26 16:17:42
字體:
來源:轉載
供稿:網(wǎng)友
今天想把一個用使用了HttpClient的自動簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包。翻Android的文檔時發(fā)現(xiàn)官方還提供了一個實現(xiàn)了HttpClient接口的AndroidHttpClient,上網(wǎng)搜了下沒發(fā)現(xiàn)關于AndroidHttpClient的文章。當然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好。
下面是2個測試用的HttpServlet
復制代碼 代碼如下:

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>
}

復制代碼 代碼如下:

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的值,這時瀏覽器會得到一個用于定位服務端session的cookie。然后訪問Info,如果有cookie的話服務端能找到剛才你傳的值并返回給你,沒帶cookie的話就不能找到。
Android端代碼:
復制代碼 代碼如下:

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)建新實例。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 97人人草 | 一本一本久久a久久精品综合小说 | 国产亚洲精品久久久闺蜜 | 叶子楣成人爽a毛片免费啪啪 | 国产美女的小嫩bbb图片 | av电影免费观看 | 精品国产91久久久 | 成人免费观看在线视频 | 国产精品久久久久久久久久10秀 | 欧美精品网址 | 黄色免费在线视频网站 | 羞羞视频.www在线观看 | 国产91一区二区三区 | 欧美一级高清片在线 | 色999久久久精品人人澡69 | 史上最强炼体老祖动漫在线观看 | 日韩视频在线一区二区三区 | 韩国精品一区二区三区四区五区 | 成年人高清视频在线观看 | 国产精品久久久毛片 | 国产1区2区3区中文字幕 | 黄色成人av在线 | 制服丝袜成人动漫 | 狠狠久久伊人中文字幕 | 日韩av电影免费看 | 久久亚洲美女视频 | 日韩av电影在线免费观看 | av免费在线观看av | 亚洲一区二区观看播放 | 国产一区二区在线免费播放 | 原来神马影院手机版免费 | 性猛交ⅹxxx乱巴西 欧美日韩1区2区3区 | 成人在线观看免费观看 | av电影直播 | 成人做爰高潮片免费视频韩国 | 久久亚洲春色中文字幕久久 | 成人毛片在线免费看 | 免费国产不卡午夜福在线 | 国产精品久久久麻豆 | 欧美日韩免费一区二区三区 | 国产精品久久久久久久久久久久久久久久 |