`
jiasudu1649
  • 浏览: 709547 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

httpclient get post到一个php网站

阅读更多
java 代码
 
  1. import java.io.IOException;  
  2.   
  3.   
  4.   
  5. import org.apache.commons.httpclient.*;  
  6.   
  7. import org.apache.commons.httpclient.methods.*;  
  8.   
  9.   
  10. public class SimpleHttpClient {  
  11.   
  12.   
  13.   
  14.     public static void main(String[] args) throws IOException  
  15.   
  16.     {  
  17.   
  18.         HttpClient client = new HttpClient();  
  19.   
  20.   
  21.   
  22.         HttpMethod method = getGetMethod();  
  23. //        HttpMethod method = getPostMethod();//使用POST方式提交数据  
  24.   
  25.         client.executeMethod(method);  
  26.   
  27.        //打印服务器返回的状态  
  28.   
  29.         System.out.println(method.getStatusLine());  
  30.   
  31.         //打印结果页面  
  32.   
  33.         String response =  
  34.   
  35.            new String(method.getResponseBodyAsString().getBytes("8859_1"));  
  36.   
  37.        //打印返回的信息  
  38.   
  39.         System.out.println(response);  
  40.   
  41.         method.releaseConnection();  
  42.   
  43.     }  
  44.   
  45.     /** 
  46.  
  47.      * 使用GET方式提交数据 
  48.  
  49.      * @return 
  50.  
  51.      */  
  52.   
  53.     private static HttpMethod getGetMethod(){  
  54.         String path = "D:\\data\\uploaddata.txt";  
  55.         return new GetMethod("http://localhost/Simple/clientsetdata.php/clientsetdata.php?path="+path);  
  56.   
  57.     }  
  58.   
  59.     /** 
  60.  
  61.      * 使用POST方式提交数据 
  62.  
  63.      * @return 
  64.  
  65.      */  
  66.   
  67.     private static HttpMethod getPostMethod(){  
  68.   
  69.         PostMethod post = new PostMethod("/clientsetdata.php");  
  70.   
  71.         NameValuePair age= new NameValuePair("age","99");  
  72.   
  73.         post.setRequestBody(new NameValuePair[] { age});  
  74.   
  75.         return post;  
  76.   
  77.     }  
  78.   
  79. }  

clientsetdata.php 代码
 
  1. php  
  2.   
  3. //链接数据库  
  4. $user = "root";  
  5. $pass = "";  
  6. $db = "sss";  
  7. $link =  mysql_connect( "localhost", $user, $pass );  
  8.   
  9. if ( ! $link ){  
  10.     die( "Couldn't connect to MySQL" );  
  11. }  
  12.   
  13. mysql_select_db( $db, $link ) or die ( "Couldn't open $db: ".mysql_error() );  
  14.   
  15. $path=$_GET['path'];  
  16. // echo "$path";  
  17.    $userfilefile_get_contents($path);  
  18.   echo $userfile;  
  19.   
  20.   
  21. mysql_close( $link );  
  22.   
  23.   ?>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics