apache HttpClient 4.5.2 模拟请求http

简介

借鉴地址

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可:

  1. 创建CloseableHttpClient对象。
  2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
  3. 如果需要发送请求参数,可可调用setEntity(HttpEntity entity)方法来设置请求参数。setParams方法已过时(4.4.1版本)。
  4. 调用HttpGet、HttpPost对象的setHeader(String name, String value)方法设置header信息,或者调用setHeaders(Header[] headers)设置一组header信息。
  5. 调用CloseableHttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个CloseableHttpResponse。
  6. 调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容;调用CloseableHttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头。
  7. 释放连接。无论执行方法是否成功,都必须释放连接

实际使用场景:

pom依赖:

<!--apache模拟http请求-->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

测试类:

import com.jie.common.file.FileUtils;
import com.jie.common.httprequest.HttpClientUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: wugong.jie
 * \* Date: 2018/3/9 16:20
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class HttpRequestDemo {

    @Test
    public void httpRequestTest(){
        String filePath = "D:\\tmp\\alibaba.html";
        // 地址是我alibaba提供的StringUtils中的main方法里获取的测试地址......
        String postUrl = "http://www.alibaba.com/trade/search";
        Map<String,String> paramMap = new HashMap<String,String>();
        paramMap.put("fsb","y");
        paramMap.put("IndexArea","product_en");
        paramMap.put("CatId","");
        paramMap.put("SearchText","test");
        try {
            FileUtils.writeTxtToFile(HttpClientUtils.simplePost(postUrl,paramMap,""),filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

工具类:

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: wugong.jie
 * \* Date: 2018/3/9 15:55
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class HttpClientUtils {

    private static Log log = LogFactory.getLog(HttpClientUtils.class);

    private final static String DEFAULT_ENCODING = "UTF-8";
    private final static int DEFAULT_CONNECT_TIMEOUT = 5000; // 设置连接超时时间,单位毫秒
    private final static int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 1000;// 设置从connect Manager获取Connection 超时时间,单位毫秒
    private final static int DEFAULT_SOCKET_TIMEOUT = 5000;// 请求获取数据的超时时间,单位毫秒 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用

    /**
     * 简单http post请求
     * @author wugong
     * @date 2018/3/9 16:18
     * @modify if true,please enter your name or update time
     * @param
     */
    public static String simplePost(String url, Map<String,String> paramMap, String encoding) throws ParseException, IOException {
        String body = "";
        encoding = StringUtils.isBlank(encoding)?DEFAULT_ENCODING:encoding;
        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = postForm(paramMap,url,DEFAULT_ENCODING);
        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpPost);
            //获取结果实体
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                //按指定编码转换结果实体为String类型
                body = EntityUtils.toString(entity, encoding);
            }
            EntityUtils.consume(entity);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
            log.error("简单post请求遇到UnSpEcode异常",e);
            throw new IOException(e);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("简单post请求遇到IO异常",e);
            throw new IOException(e);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("简单post请求遇到PE异常",e);
            throw new ParseException();
        } finally{
            //释放链接
            response.close();
        }
        return body;
    }

    /**
     * post请求url与请求参数组装
     * @author wugong
     * @date 2018/3/9 16:02
     * @modify if true,please enter your name or update time
     * @param
     */
    private static HttpPost postForm(Map<String,String> paramMap,String url,String encoding) throws UnsupportedEncodingException {
        HttpPost httpPost = new HttpPost(url);
        //装填参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        if(paramMap!=null){
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        //设置参数到请求对象中
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT).setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
                .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).build();
        httpPost.setConfig(requestConfig);
        return httpPost;
    }
}
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: wugong.jie
 * \* Date: 2018/3/9 16:36
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class FileUtils {
    private static final Log LOG = LogFactory.getLog(FileUtils.class);
    /**
     * 纯文本文件内容写入文件file
     * @author wugong
     * @date 2018/3/9 16:38
     * @modify if true,please enter your name or update time
     * @param
     */
    public static void writeTxtToFile(String txt,String filePath) throws IOException {
        try {
            File file = new File(filePath);
            if (!file.exists()) {//如果文件不存在
                try {
                    file.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            FileOutputStream fos = new FileOutputStream(file);
            // 通过文件输入流,写入文件内容
            fos.write(txt.getBytes("utf-8"));
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            LOG.error("纯文本文件写入file遇到IO异常", e);
            throw new IOException(e);
        }
    }

}

 

转载于:https://my.oschina.net/wugong/blog/1632456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值