概述
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 );
- ?>
最后
以上就是积极蜜蜂为你收集整理的httpclient get post到一个php网站的全部内容,希望文章能够帮你解决httpclient get post到一个php网站所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复