zoukankan      html  css  js  c++  java
  • HttpClient的GET请求(post)请求

    一。不带参数的GET请求

    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 创建http GET请求
    HttpGet httpGet = new HttpGet("http://www.baidu.com/");

    CloseableHttpResponse response = null;
    try {
    // 执行请求
    response = httpclient.execute(httpGet);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println("内容长度:"+content.length());
    }
    } finally {
    if (response != null) {
    response.close();
    }
    httpclient.close();
    }

     

    二。带参数的GET请求

    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 定义请求的参数
    URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();

    System.out.println(uri);

    // 创建http GET请求
    HttpGet httpGet = new HttpGet(uri);

    CloseableHttpResponse response = null;
    try {
    // 执行请求
    response = httpclient.execute(httpGet);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    }
    } finally {
    if (response != null) {
    response.close();
    }
    httpclient.close();
    }

     

     三。不带参数的POST请求

    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 创建http POST请求
    HttpPost httpPost = new HttpPost("http://www.oschina.net/");
    //设置请求头信息,可以伪装成浏览器访问
    httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");

    CloseableHttpResponse response = null;
    try {
    // 执行请求
    response = httpclient.execute(httpPost);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    }
    } finally {
    if (response != null) {
    response.close();
    }
    httpclient.close();
    }

     

     四。带参数的POST请求

    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 创建http POST请求
    HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

    // 设置2个post参数,一个是scope、一个是q
    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);
    // 将请求实体设置到httpPost对象中
    httpPost.setEntity(formEntity);

    CloseableHttpResponse response = null;
    try {
    // 执行请求
    response = httpclient.execute(httpPost);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    }
    } finally {
    if (response != null) {
    response.close();
    }
    httpclient.close();
    }

     五。如果考虑性能问题,可以跟创建数据库连接池的做法一样,这里创建一个连接池;例子如下:

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // 设置最大连接数
    cm.setMaxTotal(200);
    // 设置每个主机地址的并发数
    cm.setDefaultMaxPerRoute(20);

    doGet(cm);

    doGet(cm);

    //这里是调用

    public static void doGet(HttpClientConnectionManager cm) throws Exception {
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

    // 创建http GET请求
    HttpGet httpGet = new HttpGet("http://www.baidu.com/");

    CloseableHttpResponse response = null;
    try {
    // 执行请求
    response = httpClient.execute(httpGet);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println("内容长度:" + content.length());
    }
    } finally {
    if (response != null) {
    response.close();
    }
    // 此处不能关闭httpClient,如果关闭httpClient,连接池也会销毁
    // httpClient.close();
    }
    }

  • 相关阅读:
    二叉树中序遍历 2021.4.14
    简单题 好数对题目 2021.4.13
    2021.4.3 在排序数组中查找元素的第一个和最后一个位置
    力扣刷题记录2021.4.13 最富有客户的资产总量
    iOS开源项目阅读整理
    Missing iOS Distribution signing identity解决方案
    微信票选项目遇到的坑
    [转]从此爱上iOS Autolayout
    [转]iOS应用性能调优的25个建议和技巧
    iOS中用json接收图片的二进制流
  • 原文地址:https://www.cnblogs.com/lingtiaoti/p/9535612.html
Copyright © 2011-2022 走看看