Java 使用HttpClient进行模拟请求

一、 环境

jdk版本: jdk1.8+

HttpClient版本: httpClient4.5+

 

推荐一个Java学习的网站  : http://how2j.cn?p=17361

 

二、 下载jar包

1、 下载HttpClient的jar包,在apache官网

       下载地址: http://hc.apache.org/downloads.cgi

 

三、 使用步骤(转)

  1.  创建HttpClient对象,HttpClients.createDefault()。
  2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象,HttpPost httpPost = new HttpPost(url)。
  3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();valuePairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));httpPost.setEntity(formEntity)。
  4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
  5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
  6. 释放连接。无论执行方法是否成功,都必须释放连接

      HttpClient在4.3版本以后声明HttpClient的方法和以前略有区别,不再是直接声明new DefaultHttpClient() .

 

四、 代码示例

package util;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
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.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 模拟请求工具类
 */
public class HttpUtil {

    /**
     * http get请求
     *
     * @param url
     * @return
     */
    public static String httpGet(String url) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        return httpGet(url, null, false);
    }

    /**、
     * https get 请求
     * @param url
     * @return
     * @throws ClassNotFoundException
     * @throws KeyManagementException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws IOException
     */
    public static String httpsGet(String url) throws ClassNotFoundException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException {
        return httpGet(url, null, true);
    }

    /**
     * http post 请求
     * @param url
     * @param params
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws KeyManagementException
     */
    public static String httpPost(String url, HashMap<String, String> params) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        return httpPost(url, params, null, false);
    }

    /**
     * https post 请求
     * @param url
     * @param params
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws KeyManagementException
     */
    public static String httpsPost(String url, HashMap<String, String> params) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        return httpPost(url, params, null, true);
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: httpGet
     * @Description: TODO(http get请求)
     */
    public static String httpGet(String url, HashMap<String, Object> maps, boolean https) throws IOException, ClassNotFoundException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        // 创建HttpClient上下文
        HttpClientContext context = HttpClientContext.create();

        // 创建一个CloseableHttpClient
        CloseableHttpClient httpClient = null;
        if(https){
            httpClient = getCloseableHttpClient();
        }else{
            httpClient = HttpClients.createDefault();
        }
        // get method
        HttpGet httpGet = new HttpGet(url);

        //设置header
        if (maps != null) {
            if (maps.containsKey(HttpHeaders.REFERER)) {
                httpGet.setHeader(HttpHeaders.REFERER, (String) maps.get(HttpHeaders.REFERER));
            }
            if (maps.containsKey(HttpHeaders.HOST)) {
                httpGet.setHeader(HttpHeaders.HOST, (String) maps.get(HttpHeaders.HOST));
            }
            if (maps.containsKey(HttpHeaders.USER_AGENT)) {
                httpGet.setHeader(HttpHeaders.USER_AGENT, (String) maps.get(HttpHeaders.USER_AGENT));
            }
            if (maps.containsKey(HttpHeaders.ACCEPT)) {
                httpGet.setHeader(HttpHeaders.ACCEPT, (String) maps.get(HttpHeaders.ACCEPT));
            }
        }

        //response
        CloseableHttpResponse response = null;
        //get response into String
        String result = "";

        response = httpClient.execute(httpGet, context);
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity, "UTF-8");

        //释放连接
        httpGet.releaseConnection();
        httpClient.close();
        return result;
    }

    /**
     * 创建CloseableHttpClient
     * @return
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    private static CloseableHttpClient getCloseableHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        // 全局请求设置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();

        //SSL 过滤
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return true;
            }
        });
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", trustAllHttpsCertificates()).build();
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
        manager.setMaxTotal(200);

        // http 请求默认设置
        HttpClientBuilder custom = HttpClients.custom();
        custom.setDefaultRequestConfig(globalConfig);
        custom.setSSLSocketFactory(sslConnectionSocketFactory);
        custom.setConnectionManager(manager);
        custom.setConnectionManagerShared(true);
        return custom.build();
    }

    /**
     * SSL  https 构建
     * @return
     */
    private static SSLConnectionSocketFactory trustAllHttpsCertificates() {
        SSLConnectionSocketFactory socketFactory = null;
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("TLS");//sc = SSLContext.getInstance("TLS")
            sc.init(null, trustAllCerts, null);
            socketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
            //HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        return socketFactory;
    }

    static class miTM implements TrustManager, X509TrustManager {

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            //don't check
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            //don't check
        }

    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: httpPost
     * @Description: TODO(http post请求)
     */
    public static String httpPost(String url, HashMap<String, String> params, HashMap<String, Object> config, boolean https) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        //httpClient
        //HttpClient httpClient = getCloseableHttpClient();
        HttpClient httpClient = null;
        if(https){
            httpClient = getCloseableHttpClient();
        }else{
            httpClient = HttpClients.createDefault();
        }

        // get method
        HttpPost httpPost = new HttpPost(url);

        //设置header
        if (config != null) {
            if (config.containsKey(HttpHeaders.REFERER)) {
                httpPost.setHeader(HttpHeaders.REFERER, (String) config.get(HttpHeaders.REFERER));
            }
            if (config.containsKey(HttpHeaders.HOST)) {
                httpPost.setHeader(HttpHeaders.HOST, (String) config.get(HttpHeaders.HOST));
            }
            if (config.containsKey(HttpHeaders.USER_AGENT)) {
                httpPost.setHeader(HttpHeaders.USER_AGENT, (String) config.get(HttpHeaders.USER_AGENT));
            }
            if (config.containsKey(HttpHeaders.ACCEPT)) {
                httpPost.setHeader(HttpHeaders.ACCEPT, (String) config.get(HttpHeaders.ACCEPT));
            }
            if(config.containsKey(HttpHeaders.CONTENT_TYPE)){
                httpPost.setHeader(HttpHeaders.CONTENT_TYPE, (String) config.get(HttpHeaders.CONTENT_TYPE));
            }
            if(config.containsKey("Origin")){
                httpPost.setHeader("Origin", (String) config.get("Origin"));
            }
        }

        //set params post参数
        List<NameValuePair> listParams = new ArrayList<NameValuePair>();

        //添加post参数
        for (Map.Entry<String, String> entry : params.entrySet()) {
            listParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        String result = "";
        try {
            //response
            httpPost.setEntity(new UrlEncodedFormEntity(listParams, "UTF-8"));
            HttpResponse response = null;
            response = httpClient.execute(httpPost);
            //get response into String

            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            httpPost.releaseConnection();
            ((CloseableHttpClient) httpClient).close();
        }
        return result;
    }


}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您也可以使用Apache HttpClient库来模拟发送POST请求,并将JSON数据作为请求体发送。以下是一个示例代码: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import java.io.BufferedReader; import java.io.InputStreamReader; public class PostJsonRequest { public static void main(String[] args) { try { // 设置请求URL和JSON数据 String url = "http://example.com/api"; String json = "{\"name\": \"John\", \"age\": 30}"; // 创建HttpClient和HttpPost对象 HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(url); // 将JSON数据作为请求体发送 StringEntity entity = new StringEntity(json); post.setEntity(entity); post.setHeader("Content-type", "application/json"); post.setHeader("Accept", "application/json"); // 发送请求并获取响应 HttpResponse response = client.execute(post); // 打印响应内容 BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述示例中,我们使用了Apache HttpClient库来创建HttpClient和HttpPost对象,并将JSON数据作为请求体发送。注意:在实际使用时,您需要将URL和JSON数据替换为实际的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值