public String myRequest() throws IOException, URISyntaxException{
        
        String url="http://localhost:8080/testspring/myResponse";
        ClientHttpRequest request = new SimpleClientHttpRequestFactory().createRequest(new URI(url), HttpMethod.POST);
        request.getHeaders().set("Content-Type", "application/json;charset=gbk");
        String json = "{\"name\":\"monkey\",\"password\":\"1234\"}";
        request.getBody().write(json.getBytes("gbk"));
        ClientHttpResponse response = request.execute();
        InputStream is = response.getBody();
        byte[] b = new byte[(int) response.getHeaders().getContentLength()];
        is.read(b);
        String s = new String(b,"gbk");
        System.out.println(s);
        
        return null;

}

 

这里的uri必须要用绝对路径。

媒体格式网上自行百度

 

public void myResponse(HttpServletRequest request,HttpServletResponse response) throws IOException{
        InputStream is = request.getInputStream();
        byte[] bytes = new byte[request.getContentLength()];
        is.read(bytes);
        String string = new String(bytes,request.getCharacterEncoding());
        System.out.println(string);
        response.getWriter().write("success");
        
    }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-07-12
  • 2021-05-24
  • 2021-08-17
  • 2021-07-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案