Java語言使用HttpClient類比瀏覽器登入,javahttpclient

來源:互聯網
上載者:User

Java語言使用HttpClient類比瀏覽器登入,javahttpclient

    使用HttpClient來類比瀏覽器登入網站,然後可以進行操作,比如發布資訊等

    第一步:擷取實際的post網址,(不考慮複雜情況下)

  1、需要使用到firefox的httpfox外掛程式,httpfox中clear一下,然後start開始捕獲

  2、切換回網頁的登入頁面,開始輸入自己的帳號密碼登入,登入成功後切回httpfox中stop,查看最近的post方法中包含的Post Data資料,和此post方法的url網址,

  3、這樣就得到了類比登入時需要Post的資料參數(Parameter)值(Value),以及實際Post的網址URL

    第二步,使用HttpClient來登入

  1、簡單核心代碼如下

  

 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         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.