Java编写http请求发送Json报文案例

时间:2022-07-24
本文章向大家介绍Java编写http请求发送Json报文案例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

小编最近又开始研究了一下http发送json报文,当然code起来非常简单,但是调试费了我老大劲儿,小编再次带着小伙们走一遍小编踏过的坑。

根据百度的解决方案,为了解决如下图的问题,小编浑身解数,下载了各种版本的jar包,然后报错的问题一字不差。

Code如下:

import net.sf.json.JSONObject;

publicclass test4 {

       publicstaticvoid  main(String[] args)

    {

         JSONObject obj = new JSONObject();

        obj.put("msg", "hi");

        obj.put("path", "/lsh/HelloWorld.jsp");

       System.out.println(obj);

    }

}

报错如下:

根据百度解决方案要下载如下几个包

于是小编下载了如下的包,没有更全,Σ( ° △ °|||)︴

然而并没有什么作用,于是小编放弃了,改用IDEA,于是问题迎刃而解

创建maven项目,增加pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>lsh</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <!-- 引入json依赖  -->
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ezmorph</groupId>
        <artifactId>ezmorph</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.20</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20140107</version>
    </dependency>
</dependencies>
</project>

加载jar后

import com.alibaba.fastjson.JSONObject;
public class test1 {
    public static void main(String[] args)
    {
        JSONObject obj = new JSONObject();
        obj.put("msg", "HELLO");
        obj.put("path", "/HelloWorld.jsp");
        System.out.println(obj);
    }
}

运行一下:成功

附上http请求json报文实例

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class test2 {

    public static Log log = LogFactory.getLog(test2.class);
    public static void json_z(){

        String resp= null;
        JSONObject obj = new JSONObject();
        //bj.put("name", "李四");
       // obj.put("age", "90");
        obj.put("name", "张三");
        obj.put("age", "18");
        String query = obj.toString();
        log.info("发送到URL的报文为:");
        log.info(query);
        try {
            URL url = new URL("http://10.8.18.161:8080/atp/mock/com/test/check?");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type","application/json");
            connection.connect();

            try (OutputStream os = connection.getOutputStream()) {
                os.write(query.getBytes("UTF-8"));
            }

            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()))) {
                String lines;
                StringBuffer sbf = new StringBuffer();
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sbf.append(lines);
                }
                log.info("返回来的报文:"+sbf.toString());
                resp = sbf.toString();

            }
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //JSONObject json =JSON.parseObject(resp);

            JSONObject json = JSONObject.fromObject(resp);
        }
    }
    public static void main(String[] args) {
        json_z();
    }
}

此处高能,链接发送我们“前沿自动化测试平台的mock模块”

运行结果: