1、HttpClient的Post方法:
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- public class TestHttpPost {
- public static void main(String[] args) {
- //1.创建默认的HttpClient实例
- HttpClient httpClient=new DefaultHttpClient();
- //2.创建HttpPost请求
- HttpPost httpPost=new HttpPost("https://api.weibo.com/oauth2/access_token");
- //3.创建参数队列
- List<NameValuePair> formparams=new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair("client_id", "xxxxxxxx"));
- formparams.add(new BasicNameValuePair("client_secret", "xxxxxxxxx"));
- formparams.add(new BasicNameValuePair("grant_type", "authorization_code"));
- formparams.add(new BasicNameValuePair("redirect_uri", "xxxxxxxxx"));
- formparams.add(new BasicNameValuePair("code","xxxxxxxxx));
- UrlEncodedFormEntity urlEncodedFormEntity;
- try {
- //4.url进行编程
- urlEncodedFormEntity=new UrlEncodedFormEntity(formparams,"UTF-8");
- //5.给httpPost请求添加实体
- httpPost.setEntity(urlEncodedFormEntity);
- //6.获得响应
- HttpResponse httpResponse=httpClient.execute(httpPost);
- //7.获得响应实体
- HttpEntity httpEntity=httpResponse.getEntity();
- //8.获得响应内容
- System.out.println(EntityUtils.toString(httpEntity));
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- //9.关闭连接,释放资源
- httpClient.getConnectionManager().shutdown();
- }
- }
- }
2、HttpClient的Get方法:
- import java.io.IOException;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- public class TestHttpGet {
- public static void main(String[] args) {
- //1.创建HttpClient实例
- HttpClient httpClient=new DefaultHttpClient();
- //2.创建HttpGet实例
- HttpGet httpGet=new HttpGet("http://www.baidu.com");
- System.out.println("URL:"+httpGet.getRequestLine());
- try {
- //3.执行HttpGet请求
- HttpResponse httpResponse=httpClient.execute(httpGet);
- System.out.println("StatusCode:"+httpResponse.getStatusLine().getStatusCode());
- //4.获取响应实体HttpEntity
- HttpEntity httpEntity=httpResponse.getEntity();
- //5.输出实体内容
- if(httpClient!=null)
- {
- System.out.println("-----------------------------------------------");
- System.out.println("httpResponse Content lenghth:"+httpEntity.getContentLength());
- System.out.println("httpResponse Content:"+EntityUtils.toString(httpEntity));
- System.out.println("-----------------------------------------------");
- }
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- //6.关闭连接
- httpClient.getConnectionManager().shutdown();
- }
- }
- }