获取“百度下一,你就知道“首页html

package com.dev1.pack01;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
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 org.junit.Test;

import java.io.IOException;

public class HttpClientTest {
    @Test
    public void test01() throws IOException {
        //0.创建配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(10000)//设置连接的超时时间
                .setConnectTimeout(10000)//设置创建连接的超时时间
                .setConnectionRequestTimeout(10000)//设置请求超时时间
                //.setProxy(new HttpHost("123.207.57.145",  1080, null))//添加代理
                .build();

        //1.创建HttpClient对象
        //HttpClient httpClient = new DefaultHttpClient();//不用
        //CloseableHttpClient httpClient = HttpClients.createDefault();//简单API
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();//常用

        //2.创建HttpGet请求
        String uri = "https://www.baidu.com";
        HttpGet httpGet = new HttpGet(uri);
        //或者单独给httpGet设置
        //httpGet.setConfig(requestConfig);

        //3.设置请求头
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36");

        CloseableHttpResponse response = null;
        try {
            //4.使用HttpClient发起请求
            response = httpClient.execute(httpGet);
            //5.判断响应状态码是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                //6.获取响应数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.关闭资源
            response.close();
            httpClient.close();
        }
    }
}
package com.dev1.pack01;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class JDKAPITest {
    @Test
    public void test01() throws Exception {
        //1.确定首页的URL
        URL url = new URL("https://www.baidu.com");
        //2.通过url对象获取远程连接
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        //3.设置请求方式  请求参数  请求头
        urlConnection.setRequestMethod("GET");  //设置请求方式的时候一定要大写, 默认的请求方式是GET
        //User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0");
        urlConnection.setConnectTimeout(30000); //连接超时 单位毫秒
        urlConnection.setReadTimeout(30000);   //读取超时 单位毫秒
        //4.获取数据
        InputStream in = urlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        String html = "";
        while ((line = reader.readLine()) != null) {
            html +=  line + "\n";
        }
        System.out.println(html);
        in.close();
        reader.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值