java入门爬虫(二)

本篇主要说明一下get请求和post请求
get请求分为有参数和无参数
get请求无参数

 public static void main(String[] args) {
        //使用HttpClient发起请求,获取response
        CloseableHttpResponse response=null;
        //创建HttpClient对象,相当于创建浏览器
        CloseableHttpClient httpClient= HttpClients.createDefault();
        //创建HttpGet对象,设置URL访问地址
        HttpGet httpGet=new HttpGet("https://data.sh.gov.cn/");


        try {
            //使用HttpClient发起请求,获取response
            response=httpClient.execute(httpGet);
            //判断状态码
            if(response.getStatusLine().getStatusCode()==200){
                //解析响应
                String content=EntityUtils.toString(response.getEntity(),"UTF-8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            /* 释放资源 */
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    //关闭浏览器
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

get请求有参数,主要使用uriBuilder.setParameter(“keys”,“value”),如果想设置多参数,可以直接在后面追加.setParameter(“keys”,“value”)…

public static void main(String[] args) throws Exception {
        //使用HttpClient发起请求,获取response
        CloseableHttpResponse response=null;
        //创建HttpClient对象,相当于创建浏览器
        CloseableHttpClient httpClient= HttpClients.createDefault();
        //设置请求地址是http://yun.itheima.com/search?keys=Java
        //创建URIBuilder
        URIBuilder uriBuilder=new URIBuilder("http://yun.itheima.com/search");
        //设置参数
        uriBuilder.setParameter("keys","Java");
        //设置多参数
        //uriBuilder.setParameter("keys","Java").setParameter("","");
        //创建HttpGet对象,设置URL访问地址
        HttpGet httpGet=new HttpGet(uriBuilder.build());
        System.out.println("发起请求的信息"+httpGet);

        try {
            //使用HttpClient发起请求,获取response
            response=httpClient.execute(httpGet);
            //判断状态码
            if(response.getStatusLine().getStatusCode()==200){
                //解析响应
                String content= EntityUtils.toString(response.getEntity(),"UTF-8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            /* 释放资源 */
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    //关闭浏览器
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

post请求也分为有参数和无参数
post请求无参数和get请求无参数很相似,只需将HttpGet改为HttpPost即可

 public static void main(String[] args) {
        //使用HttpClient发起请求,获取response
        CloseableHttpResponse response = null;
        //创建HttpClient对象,相当于创建浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建HttpGet对象,设置URL访问地址
        HttpPost httpPost = new HttpPost("http://data.sh.gov.cn/");


        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);
            //判断状态码
            if (response.getStatusLine().getStatusCode() == 200) {
                //解析响应
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            /* 释放资源 */
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //关闭浏览器
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }

post请求有参数,需要走list集合,将表单的数据进行一个简单封装再使用即可
public static void main(String[] args) throws Exception {
//使用HttpClient发起请求,获取response
CloseableHttpResponse response = null;
//创建HttpClient对象,相当于创建浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpPost对象,设置URL访问地址
HttpPost httpPost = new HttpPost(“http://yun.itheima.com/search”);

    //声明List集合,封装表单中的参数
    List<NameValuePair> params=new ArrayList<NameValuePair>();
    //设置请求地址是http://yun.itheima.com/search?keys=Java
    params.add(new BasicNameValuePair("keys","Java"));
    //创建表单的Entity对象,参数1为封装好的表单数据,参数2为编码
    UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(params,"UTF-8");
    //设置表单的Entity对象到Post请求中
    httpPost.setEntity(formEntity);
    
    try {
        //使用HttpClient发起请求,获取response
        response = httpClient.execute(httpPost);
        //判断状态码
        if (response.getStatusLine().getStatusCode() == 200) {
            //解析响应
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content.length());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        /* 释放资源 */
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭浏览器
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值