HttpClient-GET和POST請求示例

1.get請求瀏覽器

@Test
public void sendGet() {app

// 獲取鏈接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();

String entityStr = null;
CloseableHttpResponse response = null;

try {
    /*
     * 因爲GET請求的參數都是拼裝在URL地址後方,因此咱們要構建一個URL,帶參數
     */
    URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com");
    /** 第一種添加參數的形式 */
    /*uriBuilder.addParameter("name", "root");
    uriBuilder.addParameter("password", "123456");*/
    /** 第二種添加參數的形式 */
    List<NameValuePair> list = new LinkedList<>();
    BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
    BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
    list.add(param1);
    list.add(param2);
    uriBuilder.setParameters(list);

    // 根據帶參數的URI對象構建GET請求對象
    HttpGet httpGet = new HttpGet(uriBuilder.build());

    /* 
     * 添加請求頭信息
     */
    // 瀏覽器表示
    httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
    // 傳輸的類型
    httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");

    // 執行請求
    response = httpClient.execute(httpGet);
    // 得到響應的實體對象
    HttpEntity entity = response.getEntity();
    // 使用Apache提供的工具類進行轉換成字符串
    entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
    System.err.println("Http協議出現問題");
    e.printStackTrace();
} catch (ParseException e) {
    System.err.println("解析錯誤");
    e.printStackTrace();
} catch (URISyntaxException e) {
    System.err.println("URI解析異常");
    e.printStackTrace();
} catch (IOException e) {
    System.err.println("IO異常");
    e.printStackTrace();
} finally {
    // 釋放鏈接
    if (null != response) {
        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            System.err.println("釋放鏈接出錯");
            e.printStackTrace();
        }
    }
}

// 打印響應內容
System.out.println(entityStr);

}ide

2.POST請求工具

@Test
public void sendPost() {
// 獲取鏈接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();ui

String entityStr = null;
CloseableHttpResponse response = null;

try {

    // 建立POST請求對象
    HttpPost httpPost = new HttpPost("http://www.baidu.com");

    /*
     * 添加請求參數
     */
    // 建立請求參數
    List<NameValuePair> list = new LinkedList<>();
    BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
    BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
    list.add(param1);
    list.add(param2);
    // 使用URL實體轉換工具
    UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
    httpPost.setEntity(entityParam);

    /* 
     * 添加請求頭信息
     */
    // 瀏覽器表示
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
    // 傳輸的類型
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

    // 執行請求
    response = httpClient.execute(httpPost);
    // 得到響應的實體對象
    HttpEntity entity = response.getEntity();
    // 使用Apache提供的工具類進行轉換成字符串
    entityStr = EntityUtils.toString(entity, "UTF-8");

    // System.out.println(Arrays.toString(response.getAllHeaders()));

} catch (ClientProtocolException e) {
    System.err.println("Http協議出現問題");
    e.printStackTrace();
} catch (ParseException e) {
    System.err.println("解析錯誤");
    e.printStackTrace();
} catch (IOException e) {
    System.err.println("IO異常");
    e.printStackTrace();
} finally {
    // 釋放鏈接
    if (null != response) {
        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            System.err.println("釋放鏈接出錯");
            e.printStackTrace();
        }
    }
}

// 打印響應內容
System.out.println(entityStr);

}url

相關文章
相關標籤/搜索