1. 程式人生 > >JAVA 爬蟲之httpclient post請求提交表單獲取Ajax資料

JAVA 爬蟲之httpclient post請求提交表單獲取Ajax資料

public static String httpPostWithJSON(String url) throws Exception {

        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;
        
//        json方式
        JSONObject jsonParam = new JSONObject();  
        jsonParam.put("name", "admin");
        jsonParam.put("pass", "123456");
        StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解決中文亂碼問題    
        entity.setContentEncoding("UTF-8");    
        entity.setContentType("application/json");    
        httpPost.setEntity(entity);
        System.out.println();
        
    
//        表單方式
//        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>(); 
//        pairList.add(new BasicNameValuePair("name", "admin"));
//        pairList.add(new BasicNameValuePair("pass", "123456"));
//        httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));   
        
        
        HttpResponse resp = client.execute(httpPost);
        if(resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity he = resp.getEntity();
            respContent = EntityUtils.toString(he,"UTF-8");
        }
        return respContent;
    }

    
    public static void main(String[] args) throws Exception {
        String result = httpPostWithJSON("http://localhost:8080/hcTest2/Hc");
        System.out.println(result);
    }


post方式 就要考慮提交的表單內容怎麼傳輸了。本文name和pass就是表單的值了。


封裝表單屬性可以用json也可以用傳統的表單,如果是傳統表單的話 要注意,也就是在上邊程式碼註釋那部分。用這種方式的話在servlet裡也就是資料處理層可以通過request.getParameter(”string“)直接獲取到屬性值。就是相比json這種要簡單一點,不過在實際開發中一般都是用json做資料傳輸的。用json的話有兩種選擇一個是阿里巴巴的fastjson還有一個就是谷歌的gson。fastjson相比效率比較高,gson適合解析有規律的json資料。博主這裡用的是fastjson。還有用json的話在資料處理層要用流來讀取表單屬性,這就是相比傳統表單多的一點內容。程式碼下邊已經有了。


public class HcServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");  
        response.setContentType("text/html;charset=UTF-8");  
        String acceptjson = "";  
        User user = new User();
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                (ServletInputStream) request.getInputStream(), "utf-8"));  
        StringBuffer sb = new StringBuffer("");  
        String temp;  
        while ((temp = br.readLine()) != null) {  
            sb.append(temp);  
        }  
        br.close();  
        acceptjson = sb.toString();  
        if (acceptjson != "") {  
            JSONObject jo = JSONObject.parseObject(acceptjson);
            user.setUsername(jo.getString("name"));
            user.setPassword(jo.getString("pass"));
        }  
        
        request.setAttribute("user", user);
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }
}