zoukankan      html  css  js  c++  java
  • 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();

       }

      

    }

  • 相关阅读:
    第二十八节:Asp.Net Core中JWT的几种写法和认证方式
    第二十七节:安全存储机密程序
    第二十六节:扩展如何在控制台中使用HttpClientFactory、读取配置文件、数据保护、注入类
    第二十五节:数据保护程序和Hash的最佳实现(彩虹表原理)
    第二十四节:编码、解码、加密算法概念及实现(Base64、MD5、SHA、HMAC、DES、AES、RSA)
    第二十三节:Asp.Net Core中的几种安全防护
    第二十二节:Asp.Net Core中Https协议的相关配置
    第二十一节:Asp.Net Core中使用托管服务实现后台任务
    第X节:抢红包算法分享
    第四节:Geo类型介绍以及Redis批量操作、事务、分布式锁
  • 原文地址:https://www.cnblogs.com/zwjcom/p/6104035.html
Copyright © 2011-2022 走看看