Java爬虫入门到精通(二)——Get请求

一、不带参的Get请求

创建HttpGetTest.java

package crawler.test;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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 HttpGetTest {

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

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

        CloseableHttpResponse response = null;

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

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

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            关闭response和httpClient
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

输出的是日志内容和content长度

二、带参数的Get请求

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

创建HttpGetParamTest

package crawler.test;

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;

import java.io.IOException;
import java.net.URISyntaxException;

public class HttpGetParamTest {

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

        //创建URIBuilder来设置参数
        URIBuilder uriBuilder = new URIBuilder("https://so.iqiyi.com/so/q_Java");

        //设置参数,单个参数一个setParameter(),多个参数就多个setParameter
        uriBuilder.setParameter("source","input").setParameter("sr","1173401874255");

        //创建HttpGet对象,设置url访问地址
        HttpGet httpGet = new HttpGet(uriBuilder.build());

        System.out.println("发起请求:"+httpGet);

        CloseableHttpResponse response = null;

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

            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 · 访问量 483

猜你喜欢

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