首页 > 编程语言 >java发送post请求传json数据

java发送post请求传json数据

时间:2022-10-21 16:48:35浏览次数:42  
标签:java JSONObject http entity json org apache import post

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
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;
 
 
 
 
//定义发送数据
JSONObject param = new JSONObject();
param.put("username", "zhangshan");
param.put("age", "18");
//定义接收数据
JSONObject result = new JSONObject();
 
String url = "http://www.baidu.com";
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
//请求参数转JOSN字符串
StringEntity entity = new StringEntity(param.toString(), "UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
try {
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
    }
} catch (IOException e) {
    e.printStackTrace();
    result.put("error", "连接错误!");
}

 

<dependency>
        <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

测试类(无需maven的方式)

如果是放在测试类,则需要额外再加一个依赖jar包

例如如下的HttpPostTest.java:

 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
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;
 
/**
 * @author qiuqiu_xqy
 */
public class HttpPostTest {
 
    public static void main(String[] args) {
 
        //定义发送数据
        JSONObject param = new JSONObject();
        param.put("username", "zhangshan");
        param.put("age", "18");
        //定义接收数据
        JSONObject result = new JSONObject();
 
        String url = "http://www.baidu.com";
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        //请求参数转JOSN字符串
        StringEntity entity = new StringEntity(param.toString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        try {
            HttpResponse response = client.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.put("error", "连接错误!");
        }
    }
}

 

标签:java,JSONObject,http,entity,json,org,apache,import,post
From: https://www.cnblogs.com/shanhubei/p/16813945.html

相关文章

  • JavaWeb笔记(一)Java网络编程
    本笔记转自https://www.yuque.com/qingkongxiaguang/javaweb/lmkyt6青空の霞光java网络编程在JavaSE阶段,我们学习了I/O流,既然I/O流如此强大,那么能否跨越不同的主机进......
  • .net core 配置Swagger 摆脱PostMan,你值得拥有这样的api调试方式
    废话不多说直接来看第一步:安装nuget包:Swashbuckle.AspNetCore.Swagger            Swashbuckle.AspNetCore.SwaggerGen         ......
  • Java工具类
    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类。一.org.apache.commons.io.IOUtilscloseQuietly:关闭一个IO流、socket、或者s......
  • JavaScript中的bind使用技巧
    functionf(){returnthis.a;}//bind绑定会创一个与f具有相同函数体和作用域的新函数,在这个新函数中,this将会永久的绑定第一个参数。......
  • Demo39_java数组05_后半段
    //数组与增强for循环packagecom.HuanXin.array_6;publicclassDemo04{publicstaticvoidmain(String[]args){int[]A={1,2,3,4,5};//增强for......
  • 字符缓冲流复制Java文件
    packagepackage3;importjava.io.*;publicclassCopyJavaDemo1{publicstaticvoidmain(String[]args)throwsIOException{//根据数据源创建字符......
  • java_day16
    Java基础Java集合框架Map接口用于存储任意键值对key-value键:无序、无下标、不允许重复值:无序、无下标、运行重复方法put(key,value)将对象存入到集合中,关联键......
  • Java保留2位小数(六种方法)
    一、使用java.math.BigDecimal类publicstaticStringformat1(doublevalue){BigDecimalbd=newBigDecimal(value);bd=bd.setScale(......
  • 数据库 使用Kettle同步数据时报错Caused by: java.sql.SQLException: Incorrect stri
    使用kettle向mysql8.0数据库同步数据时遇到的报错,这是数据库表的编码问题。可以通过修改Mysql数据库默认字符集及相关库表、字段字符字符集解决。检查数据库表相关编码设......
  • java -jar 的脚本示例
    #!/bin/bash#chkconfig:23458090#date:2022年3月21日#启动第一个jar包PID=$(ps-ef|grepruoyi-monitor-admin.jar|grep-vgrep|awk'{print$2}')if[......