Java爬虫入门到精通(三)——Post请求

一、不带参的Post请求

创建HttpPostTest.java

package crawler.test;

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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpPostTest {

    public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象,设置url访问地址
        HttpPost httpPost = new HttpPost("https://www.baidu.com");

        CloseableHttpResponse response = null;

        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
//                用content的长度来表示程序是否执行成功
                System.out.println(content.length());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

输出日志内容和content长度

二、带参数的Post请求

实例:爱奇异中搜索Java后的url:https://so.iqiyi.com/so/q_Java?source=input&sr=1173401874255
?后面的即为参数,参数source的值为input,参数sr的值为1173401874255

创建HttpPostParam.java

package crawler.test;

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;

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

public class HttpPostParamTest {

    public static void main(String[] args) throws Exception {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象,设置url访问地址
        HttpPost httpPost = new HttpPost("https://so.iqiyi.com/so/q_Java?");

        //与Get请求不一样,Post请求需要声明List集合,封装表单中的参数
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        
        //将参数封装进表单中
        params.add(new BasicNameValuePair("source","input"));
        params.add(new BasicNameValuePair("sr","1173401874255"));

        //创建表单中的Entity对象
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"utf8");

        //设置表单中的Entity对象到Post请求中
        httpPost.setEntity(formEntity);

        CloseableHttpResponse response = null;

        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
//                用content的长度来表示程序是否执行成功
                System.out.println(content.length());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

输出日志内容和content长度

发布了13 篇原创文章 · 获赞 1 · 访问量 482

猜你喜欢

转载自blog.csdn.net/weixin_43235209/article/details/104572134