码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient请求

时间:2016-11-26 14:33:32      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:username   close   set   工具包   java   efault   add   编程工具   项目   

HttpClient

 

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,

 并且它支持 HTTP 协议最新的版本和建议。

 

使用httpClient执行get请求<url不带参的>

 

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

 

public class HttpClient {

  

   @Test

   public void doGet()throws Exception{

     //创建一个HttpClient对象

     CloseableHttpClient httpClient = HttpClients.createDefault();

     //创建一个GET对象

     HttpGet get = new HttpGet("http://www.sogou.com");

     //执行请求

     CloseableHttpResponse response = httpClient.execute(get); 

     //获取相应结果

     int statusCode = response.getStatusLine().getStatusCode();//响应的状态码 

     System.out.println(statusCode);//如果结果为200 即为正常

     HttpEntity entity = response.getEntity();//响应内容

     String string = EntityUtils.toString(entity);

     System.out.println(string);

     //关闭HttpClient

     response.close();

     httpClient.close();

    

   }

}

 

 

使用httpClient执行get请求<url带参的>

 

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.utils.URIBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

 

public class HttpClient {

  

   @Test

   public void doGetWithParam()throws Exception{

     //创建一个httpClient对象

     CloseableHttpClient httpClient = HttpClients.createDefault();

     //创建一个uri对象

     URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");

     uriBuilder.addParameter("query", "杀破狼");                                             //www.sogou.com/web?query=杀破狼

     HttpGet get = new HttpGet(uriBuilder.build());

     //执行结果

     CloseableHttpResponse response = httpClient.execute(get);

     //获取响应结果

     int statusCode = response.getStatusLine().getStatusCode();

     System.out.println(statusCode);

     HttpEntity entity = response.getEntity();

     String string = EntityUtils.toString(entity,"UTF-8");

     System.out.println(string);

     //关闭HttpClient

     response.close();

     httpClient.close();

   } 

}

 

HttpClient的post请求方式<不带参数的post请求>

 

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

 

public class HttpClient {

  

   @Test

   public void doPost()throws Exception{

     //创建一个httpClient对象

     CloseableHttpClient httpClient = HttpClients.createDefault();

     //创建一个post对象

     HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");

     //执行结果

     CloseableHttpResponse response = httpClient.execute(post);

     String string = EntityUtils.toString(response.getEntity());

     System.out.println(string);

     //关闭HttpClient

     response.close();

     httpClient.close();

   }

}

 

HttpClient使用post请求<带参数请求>

 

import java.util.ArrayList;

import java.util.List;

 

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

 

public class HttpClient {

  

   @Test

   public void doPostWithParam()throws Exception{

     //创建一个httpClient对象

     CloseableHttpClient httpClient = HttpClients.createDefault();

     //创建一个post对象

     HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");

     //创建一个Entity.模拟一个表单

     List<NameValuePair> kvList = new ArrayList<>();

     kvList.add(new BasicNameValuePair("username","abc"));

     kvList.add(new BasicNameValuePair("password","123"));

     //包装成一个Entity对象

     StringEntity entity = new UrlEncodedFormEntity(kvList);

     //设置请求内容

     post.setEntity(entity);

    

     //执行结果

     CloseableHttpResponse response = httpClient.execute(post);

     String string = EntityUtils.toString(response.getEntity());

     System.out.println(string);

     //关闭HttpClient

     response.close();

     httpClient.close();

   }

  

}

 

HttpClient请求

标签:username   close   set   工具包   java   efault   add   编程工具   项目   

原文地址:http://www.cnblogs.com/zwjcom/p/6104035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!