测试httpclient类
本文最后更新于97 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
package com.sky.test;

import com.alibaba.fastjson.JSONObject;
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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 测试httpclient类
 * @description:
 * @author sky
 */
//@SpringBootTest
public class HttpClientTest {

    /**
     * 测试通过httpclient发送GET请求
     */
    @Test
    public void testGet() throws Exception {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建HttpGet对象,设置请求地址
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        // 使用HttpClient对象发送请求,并获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 打印响应状态码
        System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        String boby = EntityUtils.toString(entity);
        // 打印响应内容
        System.out.println("响应内容为:" + boby);

        // 关闭HttpClient和HttpResponse对象
        response.close();
        httpClient.close();
    }

    /**
     * 测试通过httpclient发送POST请求
     */
    @Test
    public void testPOST() throws Exception {
        // 1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.创建HttpPost对象,设置请求地址和请求参数
        HttpPost httpPOST = new HttpPost("http://localhost:8080/admin/employee/login");
        // 3.设置请求参数
        JSONObject json = new JSONObject();
        json.put("username", "admin");
        json.put("password", "123456");
        StringEntity entity = new StringEntity(json.toString());
        //4.指定请求内容类型为json
        entity.setContentType("application/json");
        //5.指定请求参数编码格式
        entity.setContentEncoding("UTF-8");
        //6.设置请求参数
        httpPOST.setEntity(entity);
        // 7.使用HttpClient对象发送请求,并获取响应
        CloseableHttpResponse response = httpClient.execute(httpPOST);
        // 8.打印响应状态码
        System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
        HttpEntity responseEntity = response.getEntity();
        String responseBody = EntityUtils.toString(responseEntity);
        // 9.打印响应内容
        System.out.println("响应内容为:" + responseBody);
        // 10.关闭HttpClient和HttpResponse对象
        response.close();
        httpClient.close();

    }

}
觉得有帮助可以投喂下博主哦~感谢!
作者:JoyBoy
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0协议
转载请注明文章地址及作者哦~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇