远程接口的调用
1.Httpclient简介
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
2.导入相应的坐标
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
3.请求
-
3.1 执行无参数的get 请求
public static void doGet(){ // 创建Httpclient对象 CloseableHttpClient httpclient=HttpClients.createDefault(); // 创建http GET请求 HttpGet httpGet=new HttpGet("http://www.oschina.net/"); CloseableHttpResponse response=null; try{ // 执行请求 response=httpclient.execute(httpGet); System.out.println(response.getStatusLine()); // 判断返回状态是否为200 if(response.getStatusLine().getStatusCode()==200){ String content=EntityUtils.toString(response.getEntity(),"UTF-8"); System.out.println("内容长度:"+content.length()); } }catch(Exception e){ e.printStackTrace(); }finally{ if(response!=null){ try{ response.close(); }catch(IOException e){ e.printStackTrace(); } } try{ httpclient.close(); }catch(IOException e){ e.printStackTrace(); } }
-
3.2执行有参数的get请求
public static void doGetParam(){ // 创建Httpclient对象 CloseableHttpClient httpclient=HttpClients.createDefault(); CloseableHttpResponse response=null; try{ // 定义请求的参数 URI uri=new URIBuilder("http://www.baidu.com/s").setParameter("wd","数据库").build(); System.out.println(uri); // 创建http GET请求 HttpGet httpGet=new HttpGet(uri); // 执行请求 response=httpclient.execute(httpGet); // 判断返回状态是否为200 if(response.getStatusLine().getStatusCode()==200){ String content=EntityUtils.toString(response.getEntity(),"UTF-8"); System.out.println(content); } }catch(Exception e){ e.printStackTrace(); }finally{ if(response!=null){ try{ response.close(); }catch(IOException e){ // TODO Auto-generated catch block e.printStackTrace(); } } try{ httpclient.close(); }catch(IOException e){ // TODO Auto-generated catch block e.printStackTrace(); } } } ```
-
3.3无参数的post 请求
public static void doPost(){
// 创建Httpclient对象
CloseableHttpClient httpclient=HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
// 创建http POST请求
HttpPost httpPost=new HttpPost("http://www.oschina.net/");
CloseableHttpResponse response=null;
try{
// 执行请求
response=httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
// 判断返回状态是否为200
if(response.getStatusLine().getStatusCode()==200){
String content=EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(content);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(response!=null){
try{
response.close();
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try{
httpclient.close();
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 3.4 有参数的post请求
public static void doPostParam()throws Exception{
// 创建Httpclient对象
CloseableHttpClient httpclient=HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
// 创建http POST请求
HttpPost httpPost=new HttpPost("http://www.oschina.net/search");
// 设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("scope","project"));
parameters.add(new BasicNameValuePair("q","java"));
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
CloseableHttpResponse response=null;
try{
// 执行请求
response=httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
// 判断返回状态是否为200
if(response.getStatusLine().getStatusCode()==200){
String content=EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(content);
}
}finally{
if(response!=null){
response.close();
}
httpclient.close();
}
}