AndroidHttpClient 사용 쿠키 응용 분석
 
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> 
} 
 
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 의 차이 점:AndroidHttpClient 는 주 스 레 드 에서 execute 할 수 없 으 며 이상 을 던 집 니 다.AndroidHttpClient 는 정적 방법 인 new Instance 를 통 해 인 스 턴 스 를 얻 습 니 다.인 자 는 에이전트 이 고 프 록 시 를 사용 하지 않 으 면""을 작성 합 니 다.DefaultHttpClient 는 기본적으로 Cookie 를 사용 합 니 다.AndroidHttpClient 는 기본적으로 Cookie 를 사용 하지 않 습 니 다.사용 하려 면 execute 를 사용 할 때마다 HttpContext 인 자 를 추가 하고 CookieStore 를 추가 해 야 합 니 다.다 쓴 후에 close 를 잊 지 마 세 요.그렇지 않 으 면 새로운 인 스 턴 스 를 만 들 수 없습니다.
 AndroidHttpClient 와 DefaultHttpClient 의 차이 점:AndroidHttpClient 는 주 스 레 드 에서 execute 할 수 없 으 며 이상 을 던 집 니 다.AndroidHttpClient 는 정적 방법 인 new Instance 를 통 해 인 스 턴 스 를 얻 습 니 다.인 자 는 에이전트 이 고 프 록 시 를 사용 하지 않 으 면""을 작성 합 니 다.DefaultHttpClient 는 기본적으로 Cookie 를 사용 합 니 다.AndroidHttpClient 는 기본적으로 Cookie 를 사용 하지 않 습 니 다.사용 하려 면 execute 를 사용 할 때마다 HttpContext 인 자 를 추가 하고 CookieStore 를 추가 해 야 합 니 다.다 쓴 후에 close 를 잊 지 마 세 요.그렇지 않 으 면 새로운 인 스 턴 스 를 만 들 수 없습니다.
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.