HttpClient的Post与Get请求

1、HttpClient的Post方法:

  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.ClientProtocolException;  
  10. import org.apache.http.client.HttpClient;  
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.apache.http.message.BasicNameValuePair;  
  15. import org.apache.http.util.EntityUtils;  
  16.   
  17.   
  18. public class TestHttpPost {  
  19.   
  20.     public static void main(String[] args) {  
  21.           
  22.         //1.创建默认的HttpClient实例   
  23.         HttpClient httpClient=new DefaultHttpClient();  
  24.         //2.创建HttpPost请求   
  25.         HttpPost httpPost=new HttpPost("https://api.weibo.com/oauth2/access_token");  
  26.         //3.创建参数队列   
  27.         List<NameValuePair> formparams=new ArrayList<NameValuePair>();  
  28.         formparams.add(new BasicNameValuePair("client_id""xxxxxxxx"));  
  29.         formparams.add(new BasicNameValuePair("client_secret""xxxxxxxxx"));  
  30.         formparams.add(new BasicNameValuePair("grant_type""authorization_code"));  
  31.           
  32.         formparams.add(new BasicNameValuePair("redirect_uri""xxxxxxxxx"));  
  33.         formparams.add(new BasicNameValuePair("code","xxxxxxxxx));  
  34.         UrlEncodedFormEntity urlEncodedFormEntity;  
  35.           
  36.         try {  
  37.             //4.url进行编程   
  38.             urlEncodedFormEntity=new UrlEncodedFormEntity(formparams,"UTF-8");  
  39.             //5.给httpPost请求添加实体   
  40.             httpPost.setEntity(urlEncodedFormEntity);  
  41.             //6.获得响应   
  42.             HttpResponse httpResponse=httpClient.execute(httpPost);  
  43.             //7.获得响应实体   
  44.             HttpEntity httpEntity=httpResponse.getEntity();  
  45.             //8.获得响应内容   
  46.             System.out.println(EntityUtils.toString(httpEntity));  
  47.               
  48.         } catch (UnsupportedEncodingException e) {  
  49.             // TODO Auto-generated catch block   
  50.             e.printStackTrace();  
  51.         } catch (ClientProtocolException e) {  
  52.             // TODO Auto-generated catch block   
  53.             e.printStackTrace();  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block   
  56.             e.printStackTrace();  
  57.         }finally{  
  58.             //9.关闭连接,释放资源   
  59.             httpClient.getConnectionManager().shutdown();  
  60.         }  
  61.   
  62.     }  
  63.   
  64. }  

2、HttpClient的Get方法:

  1. import java.io.IOException;  
  2. import org.apache.http.HttpEntity;  
  3. import org.apache.http.HttpResponse;  
  4. import org.apache.http.client.ClientProtocolException;  
  5. import org.apache.http.client.HttpClient;  
  6. import org.apache.http.client.methods.HttpGet;  
  7. import org.apache.http.impl.client.DefaultHttpClient;  
  8. import org.apache.http.util.EntityUtils;  
  9.   
  10. public class TestHttpGet {  
  11.   
  12.     public static void main(String[] args) {  
  13.       
  14.         //1.创建HttpClient实例   
  15.         HttpClient httpClient=new DefaultHttpClient();  
  16.         //2.创建HttpGet实例   
  17.         HttpGet httpGet=new HttpGet("http://www.baidu.com");          
  18.         System.out.println("URL:"+httpGet.getRequestLine());  
  19.         try {  
  20.             //3.执行HttpGet请求   
  21.             HttpResponse httpResponse=httpClient.execute(httpGet);  
  22.               
  23.             System.out.println("StatusCode:"+httpResponse.getStatusLine().getStatusCode());  
  24.             //4.获取响应实体HttpEntity   
  25.             HttpEntity httpEntity=httpResponse.getEntity();  
  26.               
  27.             //5.输出实体内容   
  28.             if(httpClient!=null)  
  29.             {  
  30.                 System.out.println("-----------------------------------------------");  
  31.                 System.out.println("httpResponse Content lenghth:"+httpEntity.getContentLength());  
  32.                 System.out.println("httpResponse Content:"+EntityUtils.toString(httpEntity));  
  33.                 System.out.println("-----------------------------------------------");  
  34.                   
  35.             }  
  36.         } catch (ClientProtocolException e) {  
  37.             // TODO Auto-generated catch block   
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.             // TODO Auto-generated catch block   
  41.             e.printStackTrace();  
  42.         }finally{  
  43.             //6.关闭连接   
  44.             httpClient.getConnectionManager().shutdown();  
  45.         }  
  46.     }  
  47.   
  48. }  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值