• HttpClient请求


    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();

       }

      

    }

  • 相关阅读:
    LN : leetcode 217 Contains Duplicate
    LN : leetcode 53 Maximum Subarray
    day23——删除数据、更改数据、索引
    day22——创建表、增加数据、查询数据
    day21——游标、mysql连接池、设计表结构
    day20——安装客户端、数据库连接、mysql事务、mysql操作数据
    day19——常用正则表达式、re正则对象和正则匹配效率比较、编译正则对象
    day20——re的matche方法和search方法、re的split,findall,finditer方法、re的matche对象
    day18——json
    day17——logging、os模块、commands模块、sys模块
  • 原文地址:https://www.cnblogs.com/zwjcom/p/6104035.html
Copyright © 2020-2023  润新知