项城做网站,wordpress博客模板下载,网站建设报价 下载,河北邢台有几个区县一、介绍
HttpClient是Apache Jakarta Common 下的子项目#xff0c;可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包。
HttpClient 是一个HTTP通信库、一个工具包#xff0c;它只提供一个通用浏览器应用程序所期望的功能子集#xff0c;与浏览…一、介绍
HttpClient是Apache Jakarta Common 下的子项目可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包。
HttpClient 是一个HTTP通信库、一个工具包它只提供一个通用浏览器应用程序所期望的功能子集与浏览器相比是没有界面的。
二、添加依赖 !--httpclient--dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.14/version/dependency
二、测试
我们先创建一个用于测试的实体类
package com.example.fastjsondemo.model;import lombok.Data;/*** author qx* date 2023/8/29* des 测试的实体类*/
Data
public class Map {private String status;private String info;private String infocode;private String province;private String city;private String adcode;private String rectangle;
}测试Get请求 /*** 测试get请求*/Testvoid testGet() throws IOException {String url https://restapi.amap.com/v3/ip?key0113a13c88697dcea6a445584d535837ip171.110.83.78;CloseableHttpClient client HttpClients.createDefault();HttpGet httpGet new HttpGet(url);CloseableHttpResponse response client.execute(httpGet);if (response.getStatusLine().getStatusCode() HttpStatus.SC_OK) {String json EntityUtils.toString(response.getEntity());Map map JSONObject.parseObject(json, Map.class);System.out.println(map);}}
执行Get请求输出:
Map(status1, infoOK, infocode10000, province广西壮族自治区, city梧州市, adcode450400, rectangle111.1604726,23.41005092;111.4408064,23.57943575)测试Post请求
/*** 测试Post请求** throws IOException*/Testvoid testPost() throws IOException {CloseableHttpClient client HttpClients.createDefault();String url https://restapi.amap.com/v3/ip;HttpPost httpPost new HttpPost(url);// 参数设置ListNameValuePair paramList new ArrayList();paramList.add(new BasicNameValuePair(key, 0113a13c88697dcea6a445584d535837));paramList.add(new BasicNameValuePair(ip, 171.110.83.78));// 设置httpPost使用的参数httpPost.setEntity(new UrlEncodedFormEntity(paramList));// 执行CloseableHttpResponse response client.execute(httpPost);if (response.getStatusLine().getStatusCode() HttpStatus.SC_OK) {String json EntityUtils.toString(response.getEntity());Map map JSONObject.parseObject(json, Map.class);System.out.println(map);}}
执行Post请求输出
Map(status1, infoOK, infocode10000, province广西壮族自治区, city梧州市, adcode450400, rectangle111.1604726,23.41005092;111.4408064,23.57943575)