php curl post请求,java后台用request文件流方式接收

本文介绍了一种使用 PHP 的 cURL 库发送 POST 请求的方法,并展示了如何在 Java 后端通过读取输入流来获取这些请求的数据。此教程详细解释了设置请求头、参数以及在接收端解析 JSON 格式数据的具体步骤。
php curl请求:
[php]  view plain  copy
  1. <?php   
  2.       
  3.             $userid = 'test';  
  4.             $key   = 'testkey';  
  5.             $sign  = md5($userid.$key);      
  6.             $data='data';  
  7.             $data_string=json_encode(array('userid'=>$userid,'sign'=>$sign,'data'=>$data));  
  8.             $url = "http://xxx.WWW.com/quick/test";  
  9.             $ch = curl_init($url);  
  10.             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  11.             curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);//$data JSON类型字符串  
  12.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  13.             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json''Content-Length: ' . strlen($data_string)));  
  14.             $result = curl_exec($ch);  
  15.             echo "<pre>";  
  16.             print_r($result);  
  17.             echo "</pre>";  
  18.             ?>  

java 后台:
[java]  view plain  copy
  1. private static String getRequestStr(HttpServletRequest request) throws Exception {  
  2.   
  3.       StringBuilder sb = new StringBuilder();  
  4.          BufferedReader in=new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));  
  5.          String line = null;  
  6.          while ((line = in.readLine()) != null) {  
  7.              sb.append(line);  
  8.          }  
  9.   
  10.      return  sb.toString();  
  11.  }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值