使用HttpsClient来idea发送请求

1. 依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>            
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

2.使用

package com.sky;

import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.apache.http.HttpEntity;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Slf4j
@SpringBootTest
public class TestHttpClient {

    @Test
    public void doGet () throws IOException {
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建HttpGet对象
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        //3.使用HttpClient发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.解析响应
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        //5.获取响应体
        HttpEntity entity = response.getEntity();
        System.out.println(entity);
        //6.获取响应内容
        String string = EntityUtils.toString(entity);
        System.out.println(string);
        //6.关闭资源
        httpClient.close();
        response.close();
    }


    @Test
    public void doPost() throws JSONException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

        //设置请求体
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);

        //发起请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = " + statusCode);

        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity);
        System.out.println("string = " + string);

    }

}

好的,下面我为您介绍一下使用Idea技术编写一个简单的爬虫项目的步骤: 1. 创建一个新的Java项目:在Idea中,选择 File -> New -> Project,选择Java类型的项目。 2. 添加必要的依赖:在 pom.xml 文件中添加 jsoup 和 HttpClient 的依赖,这两个库是Java中常用的爬虫库。 3. 编写爬虫代码:使用Java编写爬虫代码,使用 HttpClient发送 HTTP 请求并获取响应数据,使用 jsoup 库解析 HTML 页面。 以下是一个简单的爬虫示例,用于爬取百度首页的标题: ```java import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class SimpleCrawler { public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://www.baidu.com"); String html = EntityUtils.toString(httpClient.execute(httpGet).getEntity()); Document doc = Jsoup.parse(html); String title = doc.title(); System.out.println(title); } } ``` 4. 运行爬虫代码:在Idea中,选择 Run -> Run 'SimpleCrawler',运行爬虫代码并查看结果。 以上就是使用Idea技术编写一个简单的爬虫项目的基本步骤。当然,实际的爬虫项目可能还需要更加复杂的处理逻辑和数据存储方式,这需要根据具体的需求进行设计和实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值