栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java代码模拟json字符串的发送,接收以及响应

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java代码模拟json字符串的发送,接收以及响应

package com.wuhai.testdemo;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

@SpringBootTest
class TestdemoApplicationTests {



    @Test
    void contextLoads() throws JSonException {
        JSonObject jsobj1 = new JSonObject();

        JSonObject jsobj2 = new JSonObject();

        jsobj2.put("deviceID", "112");

        jsobj2.put("channel", "channel");

        jsobj2.put("state", "0");

        jsobj1.put("item", jsobj2);

        jsobj1.put("requestCommand", "control");

        post(jsobj1, "http://192.168.1.182:1234/authentication");


    }

    public String post(JSonObject json, String url) {

        String result = "";

        try {

            HttpClient client = new DefaultHttpClient();

            HttpPost post = new HttpPost(url);

            post.setHeader("Content-Type", "appliction/json");

            post.addHeader("Authorization", "Basic YWRtaW46");

            StringEntity s = new StringEntity(json.toString(), "utf-8");

            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "appliction/json"));

            post.setEntity(s);

            HttpResponse httpResponse = client.execute(post);

            InputStream in = httpResponse.getEntity().getContent();

            BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));

            StringBuilder strber = new StringBuilder();

            String line = null;

            while ((line = br.readLine()) != null) {

                strber.append(line + "n");

            }

            in.close();

            result = strber.toString();

            if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

                result = "服务器异常";

            }

        } catch (Exception e) {

            System.out.println("请求异常");

            throw new RuntimeException(e);

        }

        System.out.println("result==" + result);

        return result;

    }

}
package com.wuhai.testdemo.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


@RestController
public class JsonReceiveController {

    @Resource
    protected HttpServletRequest request;

    @RequestMapping(value="authentication",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Map getString() throws UnsupportedEncodingException, IOException {

        System.out.println("进入=====================");

        //后台接收

        InputStreamReader reader=new InputStreamReader(request.getInputStream(),"UTF-8");

        char [] buff=new char[1024];

        int length=0;

        while((length=reader.read(buff))!=-1){

            String x=new String(buff,0,length);

            System.out.println(x);

        }

        //响应

        Map jsonObject = new HashMap(); //创建Json对象

        jsonObject.put("username", "张三");     //设置Json对象的属性

        jsonObject.put("password", "123456");

        return jsonObject;

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号