歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android入門:用HttpClient模擬HTTP的GET和POST請求

一、HttpClient介紹

HttpClient是用來模擬HTTP請求的,其實實質就是把HTTP請求模擬後發給Web服務器;Android已經集成了HttpClient,因此可以直接使用;

注:此處HttpClient代碼不只可以適用於Android,也可適用於一般的Java程序;

HTTP GET核心代碼:

(1)DefaultHttpClient client = new DefaultHttpClient();
(2)HttpGet get = new HttpGet(String url);//此處的URL為http://..../path?arg1=value&....argn=value
(3)HttpResponse response = client.execute(get); //模擬請求
(4)int code = response.getStatusLine().getStatusCode();//返回響應碼
(5)InputStream in = response.getEntity().getContent();//服務器返回的數據

HTTP POST核心代碼:

(1)DefaultHttpClient client = new DefaultHttpClient();
(2)BasicNameValuePair pair = new BasicNameValuePair(String name,String value);//創建一個請求頭的字段,比如content-type,text/plain
(3)UrlEncodedFormEntity entity = new UrlEncodedFormEntity(List<NameValuePair> list,String encoding);//對自定義請求頭進行URL編碼
(4)HttpPost post = new HttpPost(String url);//此處的URL為http://..../path
(5)post.setEntity(entity);
(6)HttpResponse response = client.execute(post);
(7)int code = response.getStatusLine().getStatusCode();
(8)InputStream in = response.getEntity().getContent();//服務器返回的數據

二、服務器端代碼

服務器端代碼和通過URLConnection發出請求的代碼不變:

  1. package org.xiazdong.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.annotation.WebServlet;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. @WebServlet("/Servlet1")  
  13. public class Servlet1 extends HttpServlet {  
  14.   
  15.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  16.         String nameParameter = request.getParameter("name");  
  17.         String ageParameter = request.getParameter("age");  
  18.         String name = new String(nameParameter.getBytes("ISO-8859-1"),"UTF-8");  
  19.         String age = new String(ageParameter.getBytes("ISO-8859-1"),"UTF-8");  
  20.         System.out.println("GET");  
  21.         System.out.println("name="+name);  
  22.         System.out.println("age="+age);  
  23.         response.setCharacterEncoding("UTF-8");  
  24.         OutputStream out = response.getOutputStream();//返回數據   
  25.         out.write("GET請求成功!".getBytes("UTF-8"));  
  26.         out.close();  
  27.     }  
  28.   
  29.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  30.         request.setCharacterEncoding("UTF-8");  
  31.         String name = request.getParameter("name");  
  32.         String age  = request.getParameter("age");  
  33.         System.out.println("POST");  
  34.         System.out.println("name="+name);  
  35.         System.out.println("age="+age);  
  36.         response.setCharacterEncoding("UTF-8");  
  37.         OutputStream out = response.getOutputStream();  
  38.         out.write("POST請求成功!".getBytes("UTF-8"));  
  39.         out.close();  
  40.           
  41.     }  
  42.   
  43. }  
Copyright © Linux教程網 All Rights Reserved