CloseableHttpResponse的使用


***************************

*這篇隨手弄出來了,很急躁,有空再改

***************************

基本邏輯是:

1、定義一個客戶端

2、定義一個方法(GET,POST等)

3,、客戶端執行這個方法並返回內容

 

public class ClientFormLogin {

    public static void main(String[] args) throws Exception {
        BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();
        try {
            HttpGet httpget = new HttpGet("http://...");
            CloseableHttpResponse response1 = httpclient.execute(httpget);
            try {
                HttpEntity entity = response1.getEntity();

                System.out.println("Login form get: " + response1.getStatusLine());

                System.out.println("Initial set of cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
                
                //輸出網頁源碼           
                String result = EntityUtils.toString(response1.getEntity(), "utf-8");  
                System.out.println(result);   
                // 關閉EntityUtils
EntityUtils.consume(entity);

} finally { response1.close(); }
} }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM