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

来源:互联网 发布:叮叮当客服平台软件 编辑:程序博客网 时间:2024/06/05 06:14
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.  }  
原创粉丝点击