当前位置: java基础教程 > 26-java爬虫 > 阅读正文

httpGet带参数请求

2021.5.1.   747 次   641字

httpGet带参数, 可以使用URIBuilder对象创建参数

关键代码如下

        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //3.创建URIBuilder,这是get请求的参数对象
        try {
            URIBuilder uriBulider = new URIBuilder("https://www.52dixiaowo.com/java/");
            //4.设置参数,参数名是s, 值是httpclient
                uriBulider.setParameter("s","httpclient");
            //5.创建httpGet对象, 路径为url路径
            HttpGet httpGet = new HttpGet(uriBulider.build());
            //6.执行get请求, 获得响应对象
            CloseableHttpResponse response = httpClient.execute(httpGet);
            //7.判断响应状态码是否200, 成功则打印内容
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity());
                System.out.println(content);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

下载此demo代码

本篇完,还有疑问?

加入QQ交流群:11500065636 IT 技术交流群