httpClient发送get和post请求

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

  

【get请求】

package com.xt.postman;
 
import org.apache.http.HttpEntity;
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 javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * @Author: TestRookie * @Date: 2021/7/18 15:26 * @Description: *
 */
public class TestGet {
    public static void main(String[] args) throws IOException {
        /**
         * 1.设置url
         * 2.设置请求方式,创建对应的请求方式对象
         * 3.创建httpClient对象
         * 4.发送请求
         * 5.获取报文对象
         * 6.报文对象格式化的输出
         */
        String url = "http://test.lemonban.com/ningmengban/app/login/login.html";
        HttpGet get = new HttpGet(url);
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();//流对象 read
        /*byte[] bytes = new byte[1024];
        while(content.read(bytes)!=-1){
            System.out.println(new String(bytes));
        }*/
        String result = EntityUtils.toString(entity);
        System.out.println(result);
    }
}

  

【post请求】

package com.xt.postman;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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 java.io.IOException;
import java.io.UnsupportedEncodingException;
 
/**
 * @Author: TestRookie * @Date: 2021/7/18 15:58 * @Description: *
 */
public class TestPost {
    public static void main(String[] args) throws IOException {
        /**
         * 1.设置url
         * 2.设置请求方式,创建对应的请求方式对象
         * 3.创建httpClient对象
         * 4.绑定参数
         * 5.发送请求
         * 6.获取报文对象
         * 7.格式化报文
         */
        String url="http://test.lemonban.com/ningmengban/mvc/user/login.json";
        HttpPost post = new HttpPost(url);
        //设置请求头 注意参数要以form表单的形式提交
        post.setHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        CloseableHttpClient client = HttpClients.createDefault();
        //绑定参数
        post.setEntity(new StringEntity("username=13311112222&password=4297f44b13955235245b2497399d7a93"));
        CloseableHttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        System.out.println(result);
    }
}

  

StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(params), "UTF-8");

posted @   iTao0128  阅读(173)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示