Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header

[java]  view plain copy
  1. 要获取网络上的网页内容有POST,和GET两种方式,Get比较简单,直接把参数放在URL结尾就OK,比如<a href="http://127.0.0.1/list.php?id=1">http://127.0.0.1/list.php?id=1</a>这个URL,问号后面的就是传送的参数,id为1。但是get有个受到浏览器支持的URL最大长度的限制,而且如果传用密码之类的东西,直接写在网址里也不安全。Post相对于Get没有长度限制,也不会把数据明文放在URL结尾。  
  2. 下面的例子是用Java发送Post请求,并把网页返回的内容输出。  
  3. 首先看接收请求的php文件源代码:  
  4. <pre class="html" name="code"><?php  
  5. @$pwd=$_POST["pwd"];  
  6. echo $pwd;  
  7. ?>   
  8. 很简单,功能就是把Post过来的pwd值输出。下面是发送POST的JAVA代码: package com.pocketdigi;  
  9.    
  10. import java.io.UnsupportedEncodingException;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.    
  14. import org.apache.http.HttpResponse;  
  15. import org.apache.http.NameValuePair;  
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  17. import org.apache.http.client.methods.HttpPost;  
  18. import org.apache.http.impl.client.DefaultHttpClient;  
  19. import org.apache.http.message.BasicNameValuePair;  
  20. import org.apache.http.protocol.HTTP;  
  21. import org.apache.http.util.EntityUtils;  
  22. /** 
  23. *JDK默认没有org.apache.http包,需要先去http://hc.apache.org/downloads.cgi下载 
  24. *下载HttpClient,解压,在Eclipse中导入所有JAR 
  25. */  
  26. public class Main {  
  27.     /** 
  28.      * @param args 
  29.      * @throws UnsupportedEncodingException  
  30.      * 这个例子为了简单点,没有捕捉异常,直接在程序入口加了异常抛出声明 
  31.      */  
  32.     public static void main(String[] args) throws Exception {  
  33.         // TODO Auto-generated method stub  
  34.         String url="http://localhost/newspaper/test/1.php";  
  35.         //POST的URL  
  36.         HttpPost httppost=new HttpPost(url);  
  37.         //建立HttpPost对象  
  38.         List<NameValuePair> params=new ArrayList<NameValuePair>();  
  39.         //建立一个NameValuePair数组,用于存储欲传送的参数  
  40.         params.add(new BasicNameValuePair("pwd","2544"));  
  41.         //添加参数  
  42.         httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));  
  43.         //设置编码  
  44.         HttpResponse response=new DefaultHttpClient().execute(httppost);  
  45.         //发送Post,并返回一个HttpResponse对象  
  46.                 //Header header = response.getFirstHeader("Content-Length");  
  47.         //String Length=header.getValue();  
  48.                 // 上面两行可以得到指定的Header  
  49.         if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回  
  50.             String result=EntityUtils.toString(response.getEntity());  
  51.             //得到返回的字符串  
  52.             System.out.println(result);  
  53.             //打印输出  
  54.                        //如果是下载文件,可以用response.getEntity().getContent()返回InputStream  
  55.         }  
  56.     }  
  57. }  
  58.    
  59.   
  60. 2011524日注:处理乱码,对取得的result字符串作下转换,  
  61.   
  62. result=new String(result.getBytes("ISO-8859-1"),"GBK")  
  63.    
  64.   
  65. 网页编码为GBK  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值