httpClient的使用:普通的GET和POST请求

1、HttpGet请求响应的一般步骤

  • 1、创建HttpClient对象,可以使用HttpClients.createDefault()
  • 2、 如果是无参数的GET请求,则直接使用构造方法HttpGet(String url) 创建HttpGet对象即可;
    如果是带参数GET请求,则可以先使用URIBuilder(String url) 创建对象,再调用addParameter(String param, String value) ,或setParameter(String param, String value) 来设置请求参数,并调用build()方法构建一个URI对象。只有构造方法HttpGet(URI uri) 来创建HttpGet对象。
  • 3、创建HttpResponse ,调用HttpClient 对象的== execute(HttpUriRequest request)== 发送请求,该方法返回一个HttpResponse 。调用HttpResponse的getAllHeaders()、getHeaders(String name) 等方法可获取服务器的响应头;调用HttpResponsegetEntity() 方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。通过调用getStatusLine().getStatusCode(),可以获取响应状态码;
  • 4、释放连接。

2、导入依赖

<dependency>
     <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.5</version>
  </dependency>
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.7</version>
  </dependency>
  <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
  </dependency>

3、普通的无参数GET请求

  • 打开一个url,抓取响应结果输出成html文件
/**
 *普通的GET请求
 */
public class DoGET {
    public static void main(String[] args) throws Exception {
        // 创建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");
                //内容写入文件
                FileUtils.writeStringToFile(new File("E:\\devtest\\baidu.html"), content, "UTF-8");
                System.out.println("内容长度:"+content.length());
            }
        } finally {
            if (response != null) {
                response.close();
            }
            //相当于关闭浏览器
            httpclient.close();
        }
    }
}

4、执行带参数的GET请求

import java.io.File;
import java.net.URI;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
 * 带参数的GET请求
 * 两种方式:
 *       1.直接将参数拼接到url后面 如:?wd=java
 *       2.使用URI的方法设置参数 setParameter("wd", "java")
 */
public class DoGETParam {
    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 定义请求的参数
        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(uri);
        //response 对象
        CloseableHttpResponse response = null;
        try {
            // 执行http get请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                //内容写入文件
                FileUtils.writeStringToFile(new File("E:\\devtest\\baidu-param.html"), content, "UTF-8");
                System.out.println("内容长度:"+content.length());
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    }
}

5、执行普通的POST请求

  • 无参数的POST请求,并设置Header来伪装浏览器请求
import org.apache.commons.io.FileUtils;
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.util.EntityUtils;

import java.io.File;

/**
 * 常规post请求
 *    可以设置Header来伪装浏览器请求
 */
public class DoPOST {
    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/");
        //伪装浏览器请求
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 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");
                //内容写入文件
                FileUtils.writeStringToFile(new File("E:\\devtest\\oschina.html"), content, "UTF-8");
                System.out.println("内容长度:"+content.length());
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    }
}
```java
### 6、执行带参数的POST请求

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.http.NameValuePair;
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;

/**

  • 带有参数的Post请求
  • NameValuePair
    */
    public class DoPOSTParam {
    public static void main(String[] args) throws Exception {
    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建http POST请求
    HttpPost httpPost = new HttpPost(“http://www.oschina.net/search”);
    // 设置2个post参数,一个是scope、一个是q
    List parameters = new ArrayList(0);
    parameters.add(new BasicNameValuePair(“scope”, “project”));
    parameters.add(new BasicNameValuePair(“q”, “java”));
    // 构造一个form表单式的实体
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
    // 将请求实体设置到httpPost对象中
    httpPost.setEntity(formEntity);
    //伪装浏览器
    httpPost.setHeader(“User-Agent”,
    “Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 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”);
    //内容写入文件
    FileUtils.writeStringToFile(new File(“E:\devtest\oschina-param.html”), content, “UTF-8”);
    System.out.println(“内容长度:”+content.length());
    }
    } finally {
    if (response != null) {
    response.close();
    }
    httpclient.close();
    }
    }
    }

发布了134 篇原创文章 · 获赞 91 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_44588495/article/details/102930161