天天看點

ahttpClient

public static void main(String[] args) throws IOException {
        //1.打開浏覽器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.聲明get請求
        HttpGet httpGet = new HttpGet("http://www.baidu.com/");
        //3.發送請求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.判斷狀态碼
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            //通過EntityUtils工具類轉成字元串
            String string = EntityUtils.toString(entity, "utf-8");
            System.out.println(string);
        }
        //5.關閉資源
        response.close();
        httpClient.close();
    }      

轉載于:https://www.cnblogs.com/otways/p/11561851.html