php获取get/post请求的数据

发送请求:

这里使用的是java发送的请求,发送post请求使用了apache的httpclient开源项目。

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private static String HTTPHead = "http://localhost/stu.php";  
  2.   
  3.     /**  
  4.      * 发送get请求  
  5.      *   
  6.      * @param name  
  7.      * @param age  
  8.      */  
  9.     public void sendHttpGetRequest(String name, int age) {  
  10.         URL url = null;  
  11.         try {  
  12.             url = new URL(HTTPHead + "name=" + name + "&age=" + age);  
  13.             URLConnection conn = url.openConnection();  
  14.             InputStreamReader reader = new InputStreamReader(  
  15.                     conn.getInputStream());  
  16.             BufferedReader breader = new BufferedReader(reader);  
  17.             StringBuffer buf = new StringBuffer();  
  18.             String line = null;  
  19.             while (null != (line = breader.readLine())) {  
  20.                 buf.append(line);  
  21.             }  
  22.             reader.close();  
  23.             System.out.println(buf.toString());  
  24.         } catch (Exception e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.   
  30.     /**  
  31.      * 发送post  
  32.      *   
  33.      * @param name  
  34.      * @param age  
  35.      */  
  36.     public void sendHttpPostRequst(String name, int age) {  
  37.         BufferedReader in = null;  
  38.         try {  
  39.             // 定义HttpClient  
  40.             HttpClient client = new DefaultHttpClient();  
  41.             // 实例化HTTP方法  
  42.             HttpPost request = new HttpPost(HTTPHead);  
  43.             // 创建名/值组列表  
  44.             List<NameValuePair> parameters = new ArrayList<NameValuePair>();  
  45.             parameters.add(new BasicNameValuePair("name", name));  
  46.             parameters.add(new BasicNameValuePair("age", age + ""));  
  47.             // 创建UrlEncodedFormEntity对象  
  48.             UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(  
  49.                     parameters);  
  50.             request.setEntity(formEntiry);  
  51.             // 执行请求  
  52.             HttpResponse response = client.execute(request);  
  53.             // 获取输入流  
  54.             in = new BufferedReader(new InputStreamReader(response.getEntity()  
  55.                     .getContent()));  
  56.             StringBuffer sb = new StringBuffer("");  
  57.             String line = "";  
  58.             while ((line = in.readLine()) != null) {  
  59.                 sb.append(line);  
  60.             }  
  61.             in.close();  
  62.             String result = sb.toString();  
  63.             System.out.println(result);  
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.         } finally {  
  67.             if (in != null) {  
  68.                 try {  
  69.                     in.close();  
  70.                 } catch (Exception e) {  
  71.                     e.printStackTrace();  
  72.                 }  
  73.             }  
  74.         }  
  75.     }  

接受请求:

php接受到http请求,然后解析获取请求参数,并返回给客户端。

[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //连接服务器  
  3. $conn=mysql_connect('localhost','root','');  
  4. //状态  
  5. if(!$conn){  
  6. echo "connection failed";  
  7. exit;  
  8. }  
  9.   
  10. //选择数据库  
  11. $sql='use student';  
  12. //conn通道进行  
  13. $rs=mysql_query($sql,$conn);  
  14. //设置字符集  
  15. $sql='set names utf8';  
  16. mysql_query($sql,$conn);  
  17.   
  18. //获取GET传递的数据:http://localhost/stu.php?name="fujun"&age=20  
  19. //$s_name=isset($_GET['name'])?$_GET['name']:'';  
  20. //$s_age=isset($_GET['age'])?$_GET['age']:0;  
  21.   
  22. //获取POST传递的数据:http://localhost/stu.php   
  23. //"name=putao&age=100"  
  24. //file_get_contents:将整个文件读入一个字符串  
  25. //php://input:是个可以访问请求的原始数据的只读流。POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令  
  26. $str = file_get_contents("php://input");  
  27. $arr=array();  
  28. //parse_str:将字符串解析成多个变量  
  29. parse_str($str,$arr);  
  30. echo 'name:'.$arr['name']."<br/>".'age:'.$arr['age'];  
  31. ?>  

post的效果:



javaClient

注意:

1.倒入apache的httpclient项目jar包,不止一个,遇到类没找到的提示的时候,就该看看缺少啥包。


使用这个开源项目的原因是因为这个使用起来很方便,如果使用java.net包也行,就是麻烦一点。

(文章来源: http://blog.csdn.net/yueqinglkong/article/details/38989741)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值