【Java SE】http请求HttpClient

【Java SE】http请求HttpClient

文档:https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/fundamentals.html

依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

简单例子

public String getJsonDataFromUrl(String url, String method) {
    String json = "";
    // ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池
    // ConnectTimeout 连接建立时间,三次握手完成时间
    // SocketTimeout 等待服务端响应 数据传输过程中数据包之间间隔的最大时
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000).setConnectionRequestTimeout(60000).setSocketTimeout(60000).build();
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        if ("get".equals(method.toLowerCase())) {
            HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"); httpGet.setConfig(requestConfig); response
= httpClient.execute(httpGet); } else { List<NameValuePair> list = new ArrayList<>(); if (url.contains("?")) { int index = url.indexOf("?"); String paramStr = url.substring(index + 1); url = url.substring(0, index); String[] itemArray = paramStr.split("&"); for (String item : itemArray) { String[] pairArray = item.split("="); list.add(new BasicNameValuePair(pairArray[0], pairArray[1])); } } HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(list, Consts.UTF_8)); response = httpClient.execute(httpPost); } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { json = EntityUtils.toString(entity, "UTF-8"); } } } catch (Exception e) { e.printStackTrace(); log.error("获取数据异常", e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); log.error("关闭CloseableHttpResponse异常", e); } } } return json; }

获取重定向url

String newUrl = null;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000).setConnectionRequestTimeout(60000).setSocketTimeout(60000).build();
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
    HttpClientContext context = HttpClientContext.create();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36");
    httpGet.setHeader("Host", "www.hzqu.gov.cn");
    httpGet.setHeader("Referer", "http://www.hzqu.gov.cn/");
    httpGet.setConfig(requestConfig);
    response = httpClient.execute(httpGet, context);
    HttpHost target = context.getTargetHost();
    List<URI> redirectLocations = context.getRedirectLocations();
    URI location = URIUtils.resolve(httpGet.getURI(), target, redirectLocations);
    newUrl = location.toString();
    if (newUrl.contains(";jsessionid")) {
        newUrl = newUrl.substring(0, newUrl.indexOf(";jsessionid"));
    }
    System.out.println(newUrl);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        httpClient.close();
        response.close();
    } catch (Exception e) {

    }
}

 

posted @ 2022-05-26 15:03  翠微  阅读(70)  评论(0编辑  收藏  举报