• HttpClient使用


    1.HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包
    2.主要的功能
    (1)实现了所有 HTTP 的方法(GET,POST,PUT,DELETE 等)
    (2)支持自动转向
    (3)支持 HTTPS 协议
    (4)支持代理服务器等
    我们所用的solrj就封装了HttpClient
    可用于调用微信接口
    3.get请求
    //1 创建Httpclient对象------------------相当于打开浏览器
    CloseableHttpClient httpclient = HttpClients.createDefault();

    //2 创建http GET请求--------------------在浏览器输入uri
    HttpGet httpGet = new HttpGet("http://www.baidu.com/");

    CloseableHttpResponse response = null;
    try {
    // 3执行请求-----------enter键发送请求
    response = httpclient.execute(httpGet);
    //4 判断返回状态是否为200----------------返回成功状态码,获取内容
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println("内容长度:"+content.length());
    }
    } finally {
    if (response != null) {
    //5释放资源---------------------------关闭浏览器
    response.close();
    }
    httpclient.close();
    }
    4.带参数的GET请求
    // 定义请求的参数
    URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
    // 创建http GET请求
    HttpGet httpGet = new HttpGet(uri);
    5.POST请求(与get请求相同,只需吧请求改为post即可)
    // 创建http POST请求
    HttpPost httpPost = new HttpPost("http://www.oschina.net/");
    6.带参数的POST请求(设置form表单实体,表单需要参数,参数可以多个)
    // 设置2个post参数,
    List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
    parameters.add(new BasicNameValuePair("scope", "project"));
    parameters.add(new BasicNameValuePair("q", "java"));
    // 构造一个form表单式的实体
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
    // 将请求实体设置到httpPost对象中
    httpPost.setEntity(formEntity);
    4. 使用HttpClient调用接口
    1. 编写返回对象
    public class HttpResult
    // 响应的状态码
    private int code;

    // 响应的响应体
    private String body;
    2. 封装HttpClient常用方法
    1. // 每个方法都会用到,提取出来
    private CloseableHttpClient httpClient;

    public ApiService() {
    this.httpClient = HttpClients.createDefault();
    }
    2.带参数的get请求
    public HttpResult doGet(String url, Map<String, Object> map) throws Exception {
    // 1.创建URIBuilder
    URIBuilder uriBuilder = new URIBuilder(url);

    // 2.设置请求参数
    if (map != null) {
    // 遍历请求参数
    for (Map.Entry<String, Object> entry : map.entrySet()) {
    // 封装请求参数
    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
    }
    }

    // 3.创建请求对象httpGet
    HttpGet httpGet = new HttpGet(uriBuilder.build());

    // 4.使用httpClient发起请求
    CloseableHttpResponse response = this.httpClient.execute(httpGet);

    // 5.解析返回结果,封装返回对象httpResult
    // 5.1获取状态码
    int code = response.getStatusLine().getStatusCode();

    // 5.2 获取响应体
    // 使用EntityUtils.toString方法必须保证entity不为空
    String body = "";
    if (response.getEntity() != null) {
    body = EntityUtils.toString(response.getEntity(), "UTF-8");
    }
    HttpResult result = new HttpResult();
    result.setCode(code);
    result.setBody(body);
    return result;
    }
    3.不带参数的get
    public HttpResult doGet(String uri) throws Exception {
    return this.doGet(uri, null);
    }

    4.带参数的post请求
    //1、创建POST请求方式
    HttpPost httpPost = new HttpPost(url);
    //2、设置参数
    List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
    if(map != null){
    for (Map.Entry<String, Object> m : map.entrySet()) {
    parameters.add(new BasicNameValuePair(m.getKey(), m.getValue().toString()));
    }
    }
    //3、把参数设置成form表单的形式
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
    //4、把表单对象设置到HTTPPOST请求中
    httpPost.setEntity(formEntity);
    //5、执行,返回响应
    CloseableHttpResponse response = this.httpClient.execute(httpPost);
    //6、根据响应获取状态码和数据
    int code = response.getStatusLine().getStatusCode();
    String body = "";
    if(response.getEntity() != null){
    body = EntityUtils.toString(response.getEntity(), "utf-8");
    }
    //7、封装返回对象
    HttpResult result = new HttpResult();
    result.setCode(code);
    result.setBody(body);

    return result;

    5.不带参数的post请求
    public HttpResult doPost(String url) throws Exception {
    return this.doPost(url, null);
    }
    6.put方法和post方法类似,只需把post请求改为put请求即可
    delete方法和get方法类似,只需吧get请求改为delete请求即可

  • 相关阅读:
    UVa11218 KTV
    counting sort 计数排序
    Uva10474
    Uva110 Meta-Loopless Sorts
    Uva592 Island of Logic
    Qtwebkit flashplayer插件问题
    C++程序员的javascript教程
    Binary Search
    排列组合生成算法
    【Linux】mkdir命令
  • 原文地址:https://www.cnblogs.com/soul-wonder/p/9085811.html
Copyright © 2020-2023  润新知