java代码模拟postman请求头发送POST

import com.alibaba.fastjson.JSONObject;
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;

/**
 * HTTP工具
 *
 * @author 洛天依
 */
public class HttpUtil {

    // url连接 body json参数
    public static String sendHttpPost(String url, JSONObject body) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //装填参数
        StringEntity s = new StringEntity(body.toString(), "utf-8");
        //设置参数到请求对象中
        httpPost.setEntity(s);
        // 设置头
        httpPost.addHeader("Cookie", "gid_87f036f882014cd8=d36851103c8a9b4210732f978b24273aee6c9c3e; ls_371ef9f55b6b63dc=2bc29f8964484170");
        httpPost.addHeader("Content-type", "application/json;chartset=UTF-8");
        httpPost.addHeader("User-Agent", "PostmanRuntime/7.26.2");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Connection", "keep-alive");
        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的HttpURLConnection类来模拟Postman发送POST请求。具体步骤如下: 1. 创建URL对象,指定请求的URL地址。 2. 调用URL对象的openConnection()方法,获取HttpURLConnection对象。 3. 设置请求的方法为POST,调用setRequestMethod()方法。 4. 设置请求头,调用setRequestProperty()方法。 5. 设置请求体,调用getOutputStream()方法获取输出流,将请求参数写入输出流。 6. 发送请求,调用connect()方法。 7. 获取响应,调用getInputStream()方法获取输入流,读取响应数据。 8. 关闭连接,调用disconnect()方法。 示例代码如下: ``` import java.io.*; import java.net.*; public class PostRequestDemo { public static void main(String[] args) throws Exception { String url = "http://example.com/api"; String params = "param1=value1&param2=value2"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST con.setRequestMethod("POST"); // 设置请求头 con.setRequestProperty("User-Agent", "Mozilla/5."); con.setRequestProperty("Accept-Language", "en-US,en;q=.5"); // 设置请求体 con.setDoOutput(true); OutputStream os = con.getOutputStream(); os.write(params.getBytes()); os.flush(); os.close(); // 发送请求 con.connect(); // 获取响应 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 输出响应结果 System.out.println(response.toString()); // 关闭连接 con.disconnect(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值