httpclient的用法

maven坐标:

<dependency>
    <groupId>httpclient</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.6</version>
</dependency>
<dependency>
    <groupId>httpcore</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.6</version>
</dependency>

java代码:

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * HttpClient通过Get方式请求
 * @param urlString
 * @param params
 * @return
 */
public static String getResponseContentByGetMethod(String urlString, List<NameValuePair> params) {
    String responseBody = null;
    try{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(urlString + "?" + EntityUtils.toString(new UrlEncodedFormEntity(params), Consts.UTF_8));
        httpGet.setConfig(RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000).setSocketTimeout(30000).build());
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            responseBody = EntityUtils.toString(httpEntity, Consts.UTF_8);
        }
        // 销毁实体
        EntityUtils.consume(httpEntity);
    }catch (Exception e){
        logger.error(e);
    }
    return responseBody;
}

/**
 * HttpClient通过Post方式请求
 * @param urlString
 * @param formParams
 * @return
 */
public static String getResponseContentByPostMethod(String urlString, List<NameValuePair> formParams){
    String responseBody = null;
    try{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(urlString);
        httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000).setSocketTimeout(30000).build());
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
        //StringEntity entity1 = new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            responseBody = EntityUtils.toString(httpEntity, Consts.UTF_8);
        }
        // 销毁实体
        EntityUtils.consume(httpEntity);
    }catch (Exception e){
        logger.error(e);
    }
    return responseBody;
}


你可能感兴趣的:(httpclient的用法)