前言:

网络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方法类

 package com.HttpClient.Test;

 import java.io.IOException;
import java.util.ArrayList; 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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; import com.HttpClient.jiexi.HttpClient_jiexi; public class HttpClient_post { private String entityStr;
HttpClient_jiexi JSONTOOL = new HttpClient_jiexi(); //封装POST方法
public String post(String POST_URL,ArrayList<NameValuePair> list) { try {
//把参数放入请求体中
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(POST_URL);
httpPost.setEntity(entityParam);
//发起请求
CloseableHttpResponse response = httpClient.execute(httpPost);
//获取返回状态,并判断是否连接成功。
if (response.getStatusLine().getStatusCode()==200) {
System.out.println("连接成功");
} else {
System.out.println("连接异常");
}
// 获得响应的实体对象
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;
} }

调用POST方法类

 package com.HttpClient.Test;

 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_post post = new HttpClient_post();
private String POST_URL = "https://api.apiopen.top/searchMusic";
private String entity;
private ArrayList<NameValuePair> list; @Test
public void test1() {
// 构造list集合,并添加参数
BasicNameValuePair basicNameValuePair = new BasicNameValuePair("name", "十年");
list = new ArrayList<>();
list.add(basicNameValuePair);
// 执行请求,需要传入URL与参数
entity = post.post(POST_URL,list);
System.out.println(entity);
} }

二、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); } }

HttpClient POST/SET方法的更多相关文章

  1. UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器

    1.从相册里面选取图片 /// <summary> /// 1.1 从相册里面选取图片 /// </summary> /// <param name="send ...

  2. Android 6.0 SDK 找不到HttpClient的解决方法

    一.情况描述 在eclipse或Android Studio开发时(笔者目前只用过Android Studio),设置Android SDK的编译版本为23时,且使用了httpClient相关类的库项 ...

  3. Android HttpClient基本使用方法

    GET 方式 //先将参数放入List,再对参数进行URL编码 List<BasicNameValuePair> params = new LinkedList<BasicNameV ...

  4. HttpClient的使用方法

    使用httpClient发送请求.接收响应很简单.一般需要以下几个步骤. 第一:创建HttpClient对象: 第二:创建请求方法的实例,并指定请求URL.如果要发送GET请求,创建HttpGet对象 ...

  5. HttpClient 模拟发送Post和Get请求 并用fastjson对返回json字符串数据解析,和HttpClient一些参数方法的deprecated(弃用)的综合总结

    最近在做一个接口调用的时候用到Apache的httpclient时候,发现引入最新版本4.5,DefaultHttpClient等老版本常用的类已经过时了,不推荐使用了:去官网看了一下在4.3之后就抛 ...

  6. httpClient的post方法使用form格式数据调用接口

    Http使用post调用接口,接口只接受form表单数据格式问题? 这个问题的关键是form表单提交的是数据格式是什么样子的,使用httpClient工具类时Header信息如何写. 会影响解决问题思 ...

  7. Apache httpclient的execute方法调试

    因为工作需要,想研究一下execute执行的逻辑. 在这一行调用execute: response = getHttpClient().execute(get); getHttpClient的实现: ...

  8. Httpclient 表单,json,multipart/form-data 提交 ---总结常用的方法

    最近在项目中,一直在使用HttpClient 中的方法,这里我进行一些方法的汇总,也是结合了一些大牛写的代码,以备不时之需 官话:HttpClient 是Apache Jakarta Common 下 ...

  9. .netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析

    众所周知,微服务架构是由一众微服务组成,项目中调用其他微服务接口更是常见的操作.为了便于调用外部接口,我们的常用思路一般都是封装一个外部接口的客户端,使用时候直接调用相应的方法.webservice或 ...

随机推荐

  1. DRF之认证组件、权限组件、频率组件使用方法总结

    认证组件格式: from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  2. Java内存模型相关原则详解

    在<Java内存模型(JMM)详解>一文中我们已经讲到了Java内存模型的基本结构以及相关操作和规则.而Java内存模型又是围绕着在并发过程中如何处理原子性.可见性以及有序性这三个特征来构 ...

  3. Linux 命令之 crontab

    crontab 简介 crontab 主要用于需要管理周期执行定时任务的场景 crontab 安装 (有些系统默认已经带了 crontab,无需安装的朋友可以直接跳过本节) 安装: yum insta ...

  4. 《吊打面试官》系列-Redis哨兵、持久化、主从、手撕LRU

    你知道的越多,你不知道的越多 点赞再看,养成习惯 前言 Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行360°的刁难.作为一个在互联 ...

  5. Android 开源库 GitHub 托管

    本文微信公众号「AndroidTraveler」首发. 背景 之前给大家写过一篇文章 Android 上传开源项目到 jcenter 实战踩坑之路,分享了上传开源项目到 jcenter 上面的一些踩坑 ...

  6. NOIP模拟测试23

    这次考试又一次暴露了我很大的问题. 首先做的比较好的是这几次考试一分没挂, 但是,这也体现了更大的问题,那就是我的实力似乎也仅限于此了. 考试先拿满了暴力分(100+0+50),然后看了看T2没看懂, ...

  7. P3043 [USACO12JAN]牛联盟(并查集+数学)

    (m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...

  8. 2019年10月13日 spss习题 wangqingchao

    1.spss发行版本的说法,正确的是:B a.两年发行一个新版本    b.一年发行一个新版本 c.没有任何规律   d.三年发行一个新版本 2.哪些是spss统计分析软件的基本窗口:A a.结果查看 ...

  9. Unity入门--实用知识

    目录 1. VS适配 2.实用快捷操作 3.Unity API文档 4.项目整理 1. VS适配 让你的VS完美支持Unity的脚本编写可以让你写起C#脚本来事半功倍,比如代码补全功能,可以参考下面这 ...

  10. SQlALchemy session详解

    系列文章: Python SQLAlchemy入门教程 概念 session用于创建程序和数据库之间的会话,所有对象的载入和保存都需通过session对象 . 通过sessionmaker调用创建一个 ...