使用HttpClient來模擬瀏覽器登陸網站,而後能夠進行操做,好比發佈信息等瀏覽器
第一步:獲取實際的post網址,(不考慮複雜狀況下)post
一、須要使用到firefox的httpfox插件,httpfox中clear一下,而後start開始捕獲網站
二、切換回網頁的登陸頁面,開始輸入本身的帳號密碼登陸,登陸成功後切回httpfox中stop,查看最近的post方法中包含的Post Data數據,和此post方法的url網址,url
三、這樣就獲得了模擬登陸時須要Post的數據參數(Parameter)和值(Value),以及實際Post的網址URLspa
第二步,使用HttpClient來登陸firefox
一、簡單核心代碼以下插件
1 CloseableHttpClient httpclient = HttpClients.createDefault(); 2 List<NameValuePair> postData = new ArrayList<NameValuePair>(); 3 //這裏可能有多個參數 4 postData.add(new BasicNameValuePair("username", "username")); 5 postData.add(new BasicNameValuePair("password", "password")); 6 //URL是實際的post地址,使用httpFox獲得 7 HttpPost httppost = new HttpPost(URL); 9 try {11 httppost.setEntity(new UrlEncodedFormEntity(postData, "GBK")); 12 response = httpclient.execute(httppost); 15 } catch (IOException e) { 16 } finally { 17 closeIO(response); 18 }