ysoren 发表于 2017-3-29 12:54:18

httpclient get post到一个php网站

java 代码
 

[*]import java.io.IOException;  
[*]  
[*]  
[*]  
[*]import org.apache.commons.httpclient.*;  
[*]  
[*]import org.apache.commons.httpclient.methods.*;  
[*]  
[*]  
[*]public class SimpleHttpClient {  
[*]  
[*]  
[*]  
[*]    public static void main(String[] args) throws IOException  
[*]  
[*]    {  
[*]  
[*]        HttpClient client = new HttpClient();  
[*]  
[*]  
[*]  
[*]        HttpMethod method = getGetMethod();  
[*]//        HttpMethod method = getPostMethod();//使用POST方式提交数据  
[*]  
[*]        client.executeMethod(method);  
[*]  
[*]       //打印服务器返回的状态  
[*]  
[*]        System.out.println(method.getStatusLine());  
[*]  
[*]        //打印结果页面  
[*]  
[*]        String response =  
[*]  
[*]           new String(method.getResponseBodyAsString().getBytes("8859_1"));  
[*]  
[*]       //打印返回的信息  
[*]  
[*]        System.out.println(response);  
[*]  
[*]        method.releaseConnection();  
[*]  
[*]    }  
[*]  
[*]    /** 
[*] 
[*]     * 使用GET方式提交数据 
[*] 
[*]     * @return 
[*] 
[*]     */  
[*]  
[*]    private static HttpMethod getGetMethod(){  
[*]        String path = "D:\\data\\uploaddata.txt";  
[*]        return new GetMethod("http://localhost/Simple/clientsetdata.php/clientsetdata.php?path="+path);  
[*]  
[*]    }  
[*]  
[*]    /** 
[*] 
[*]     * 使用POST方式提交数据 
[*] 
[*]     * @return 
[*] 
[*]     */  
[*]  
[*]    private static HttpMethod getPostMethod(){  
[*]  
[*]        PostMethod post = new PostMethod("/clientsetdata.php");  
[*]  
[*]        NameValuePair age= new NameValuePair("age","99");  
[*]  
[*]        post.setRequestBody(new NameValuePair[] { age});  
[*]  
[*]        return post;  
[*]  
[*]    }  
[*]  
[*]}  


clientsetdata.php 代码
 

[*]php  
[*]  
[*]//链接数据库  
[*]$user = "root";  
[*]$pass = "";  
[*]$db = "sss";  
[*]$link =  mysql_connect( "localhost", $user, $pass );  
[*]  
[*]if ( ! $link ){  
[*]    die( "Couldn't connect to MySQL" );  
[*]}  
[*]  
[*]mysql_select_db( $db, $link ) or die ( "Couldn't open $db: ".mysql_error() );  
[*]  
[*]$path=$_GET['path'];  
[*]// echo "$path";  
[*]   $userfile= file_get_contents($path);  
[*]  echo $userfile;  
[*]
[*]  
[*]  
[*]mysql_close( $link );  
[*]  
[*]  ?>  
页: [1]
查看完整版本: httpclient get post到一个php网站