Java实现Http客户端服务端间通信

这篇博客展示了如何在Spring Boot项目中实现HTTP Post请求的客户端和服务端通信。提供了两种客户端实现方式,分别依赖httpclient和commons-httpclient库,并介绍了自定义Jackson工具类的方法,强调了导入正确依赖的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

访问Http Post请求的代码示例,项目为spring boot框架,省略不重要的部分。

1、Post服务端代码

样例1:

    @RequestMapping(value ="/testDemo1",method = RequestMethod.POST,produces={"application/xml"})
    public @ResponseBody String testDemo1(@RequestBody String body) throws IOException 
    {
    	logger.info(body);    //通过@RequestBody 获取Post 的消息体内容,另外可自行了解@RequestParam、@RequestAttribute
    	return abc;
    }
样例2:
    @RequestMapping(value ="/testDemo",method = RequestMethod.POST,produces={"application/xml"})
    public @ResponseBody String flashSingalInterface(HttpServletRequest request, HttpServletResponse response) throws IOException 
    {
    	// 读取请求内容 ----直接从request中获取
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));  
        String line = null;  
        StringBuilder sb = new StringBuilder();  
        while((line = br.readLine())!=null){  
            sb.append(line);  
        }  
      
        // 将资料解码  
        String reqBody = sb.toString();  
        String result = URLDecoder.decode(reqBody, "utf-8");  //utf-8
        
        logger.info(result);
        
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("resultCode", "200");
    	map.put("resultDesc", "nice try");
    	
    	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值