http以post方式请求时,参数为json格式的字符,被请求方不能识别该请求 获取不到数据

当你用postman进行类似这种请求时是可以获取返回数据的,

 但是如果你用java代码模拟http的post请求时将参数直接挂在url后面的时候被请求方将不会识别这种请求,那怎么样才能改正呢?

直接上代码:

工具包依赖:

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.3.2</version>
        </dependency>
//s是一个Json格式的字符    错误的示范,这样的请求将不会获取到请求的返回
		HttpRequest httpRequest = HttpRequest.post("http://127.0.0.1:8080/addUserToGroup?json="+s);

		HttpResponse execute = httpRequest.execute();
		System.err.println(execute.toString());

/////////////////正确的方式///////////////
//s是一个Json格式的字符 
		HttpRequest httpRequest = HttpRequest.post("http://127.0.0.1:8080/addUserToGroup");
		httpRequest.body("json="+s);
		HttpResponse execute = httpRequest.execute();
		System.err.println(execute.toString());

结果证明大括号{}这种符号直接在url中请求是不会被识别的,如果导致无法请求

点赞或者评论是我最大的动力,有问题欢迎留言或者联系q:1559810637  

猜你喜欢

转载自blog.csdn.net/qq_41594146/article/details/88664299