zoukankan      html  css  js  c++  java
  • HttpClient POST/GET方法

    前言:

    网络API接口:https://api.apiopen.top/searchMusic

    此API接口返回类型为JSON格式类型

    GET:从指定资源请求数据

    POST:向指定资源提交要被处理的数据

    GET与POST的区别:

    ①GET在浏览器回退时是无害的,而POST会再次提交请求。

    ②GET只支持URL编码。

    ③GET参数通过URL传递,参数直接暴露在URL中会泄露信息,POST通过Request body传递不会有这样的问题。

    ④GET请求在URL中传递参数有长度限制,POST没有长度限制。

    ⑤对POST请求会险发送Header,服务器相应成功,在发送Date,服务器再次响应,需响应两次。GET会把Header与Date一起发送到服务端,服务器只响应一次。

    一、POST方法

    创建POST方法类

     1 package com.HttpClient.Test;
     2 
     3 import java.io.IOException;
     4 import java.util.ArrayList;
     5 
     6 import org.apache.http.HttpEntity;
     7 import org.apache.http.HttpHost;
     8 import org.apache.http.NameValuePair;
     9 import org.apache.http.client.ClientProtocolException;
    10 import org.apache.http.client.config.RequestConfig;
    11 import org.apache.http.client.entity.UrlEncodedFormEntity;
    12 import org.apache.http.client.methods.CloseableHttpResponse;
    13 import org.apache.http.client.methods.HttpPost;
    14 import org.apache.http.impl.client.CloseableHttpClient;
    15 import org.apache.http.impl.client.HttpClientBuilder;
    16 import org.apache.http.util.EntityUtils;
    17 
    18 import com.HttpClient.jiexi.HttpClient_jiexi;
    19 
    20 public class HttpClient_post {
    21     
    22     private String entityStr;
    23     HttpClient_jiexi JSONTOOL = new HttpClient_jiexi();
    24     
    25     //封装POST方法
    26     public String post(String POST_URL,ArrayList<NameValuePair> list) {
    27         
    28         try {
    29             //把参数放入请求体中
    30             UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
    31             CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    32             HttpPost httpPost = new HttpPost(POST_URL);
    33             httpPost.setEntity(entityParam);
    34             //发起请求
    35             CloseableHttpResponse response = httpClient.execute(httpPost);
    36             //获取返回状态,并判断是否连接成功。
    37             if (response.getStatusLine().getStatusCode()==200) {
    38                 System.out.println("连接成功");
    39             } else {
    40                 System.out.println("连接异常");
    41             }
    42             // 获得响应的实体对象
    43             HttpEntity entity = response.getEntity();
    44             
    45             //转换成字符串
    46             entityStr = EntityUtils.toString(entity, "UTF-8");
    47             //关闭请求
    48             httpClient.close();
    49             
    50         } catch (ClientProtocolException e) {
    51             System.err.println("Http协议异常");
    52             e.printStackTrace();
    53         } catch (IOException e) {
    54             System.err.println("IO异常");
    55             e.printStackTrace();
    56         }
    57         return entityStr;
    58     }
    59 
    60 }

    调用POST方法类

     1 package com.HttpClient.Test;
     2 
     3 import java.util.ArrayList;
     4 import org.apache.http.NameValuePair;
     5 import org.apache.http.message.BasicNameValuePair;
     6 import org.json.JSONException;
     7 import org.testng.annotations.Test;
     8 
     9 public class HttpClient_case1 {
    10     
    11     HttpClient_post post = new HttpClient_post();
    12     private String POST_URL = "https://api.apiopen.top/searchMusic";
    13     private String entity;
    14     private ArrayList<NameValuePair> list;
    15     
    16     @Test
    17     public void test1() {   
    18             // 构造list集合,并添加参数
    19             BasicNameValuePair basicNameValuePair = new BasicNameValuePair("name", "十年");
    20             list = new ArrayList<>();
    21             list.add(basicNameValuePair);
    22             // 执行请求,需要传入URL与参数
    23             entity = post.post(POST_URL,list);
    24             System.out.println(entity);
    25     }
    26 
    27 }

    二、GET方法

    创建GET方法类

    package com.HttpClient.Test;
    
    import java.io.IOException;
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    public class HttpClient_get {
        
        public String entityStr ;
        public CloseableHttpResponse response ;
    
        
        public String get(String GET_URL,ArrayList<NameValuePair> list) throws URISyntaxException{try {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                URIBuilder uriBuilder = new URIBuilder(GET_URL);
                uriBuilder.setParameters(list);
                //根据带参数的URI对象构建GET请求对象
                HttpGet httpGet = new HttpGet(uriBuilder.build());
                //执行请求
                response = httpClient.execute(httpGet);
                //获得响应的实体对象
                HttpEntity entity = response.getEntity();
                //转换成字符串
                entityStr = EntityUtils.toString(entity, "UTF-8");
    
                //关闭请求
                httpClient.close();
            } catch (ClientProtocolException e) {
                System.err.println("Http协议异常");
                e.printStackTrace();
            } catch (IOException e) {
                System.err.println("IO异常");
                e.printStackTrace();
            }
            return entityStr;
            
        }
    
    }

    调用GET方法类

    package com.HttpClient.Test;
    
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONException;
    import org.testng.annotations.Test;
    
    public class HttpClient_case1 {
        
        HttpClient_get get = new HttpClient_get();
        private String URL = "https://api.apiopen.top/searchMusic";
        private String entity;
        private ArrayList<NameValuePair> list;
        
        @Test
        public void test1() throws URISyntaxException {   
                // 构造list集合
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair("name", "十年");
                list = new ArrayList<>();
                list.add(basicNameValuePair);
                // 执行请求,需要传入URL与参数
                entity = get.get(URL, list);
                System.out.println(entity);
                
        }
    
    }
  • 相关阅读:
    ArrayList源码剖析
    Java集合框架
    Java数据结构和算法(十五)——无权无向图
    Java数据结构和算法(十四)——堆
    Java数据结构和算法(十三)——哈希表
    Java数据结构和算法(十二)——2-3-4树
    Java数据结构和算法(十一)——红黑树
    Java数据结构和算法(十)——二叉树
    Java数据结构和算法(九)——高级排序
    [刷题] Leetcode算法 (2020-2-27)
  • 原文地址:https://www.cnblogs.com/yogouo/p/11949355.html
Copyright © 2011-2022 走看看