@RequestHeader(“Cookie”) String cookie,
@RequestHeader(“Connection”) String conn) {
// 打印请求头信息
System.out.println("Cookie = " + cookie);
System.out.println("Connection = " + conn);
System.out.println("Accept = " + accept);
System.out.println("Accept-Language = " + acceptLanguage);
System.out.println("Accept-Encoding = " + acceptEncoding);
System.out.println("User-Agent = " + userAgent);
return “post无参请求成功”;
}
@PostMapping(“/postWithParam”)
public String postWithParam(@RequestParam String code, @RequestParam String message) {
return "post带参请求成功,参数code: " + code + ",参数message: " + message;
}
}
6.4 创建httpClient响应结果对象
/**
-
Description: 封装httpClient响应结果
-
@author JourWon
-
@date Created on 2018年4月19日
*/
public class HttpClientResult implements Serializable {
/**
- 响应状态码
*/
private int code;
/**
- 响应数据
*/
private String content;
}
6.5 重点,编写httpclient工具类
/**
-
Description: httpClient工具类
-
@author JourWon
-
@date Created on 2018年4月19日
*/
public class HttpClientUtils {
// 编码格式。发送编码格式统一用UTF-8
private static final String ENCODING = “UTF-8”;
// 设置连接超时时间,单位毫秒。
private static final int CONNECT_TIMEOUT = 6000;
// 请求获取数据的超时时间(即响应时间),单位毫秒。
private static final int SOCKET_TIMEOUT = 6000;
/**
-
发送get请求;不带请求头和请求参数
-
@param url 请求地址
-
@return
-
@throws Exception
*/
public static HttpClientResult doGet(String url) throws Exception {
return doGet(url, null, null);
}
/**
-
发送get请求;带请求参数
-
@param url 请求地址
-
@param params 请求参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doGet(String url, Map<String, String> params) throws Exception {
return doGet(url, null, params);
}
/**
-
发送get请求;带请求头和请求参数
-
@param url 请求地址
-
@param headers 请求头集合
-
@param params 请求参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建访问的地址
URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) {
Set<Entry<String, String>> entrySet = params.entrySet();
for (Entry<String, String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
// 创建http对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
/**
-
setConnectTimeout:设置连接超时时间,单位毫秒。
-
setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
-
超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
-
setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpGet.setConfig(requestConfig);
// 设置请求头
packageHeader(headers, httpGet);
// 创建httpResponse对象
CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果
return getHttpClientResult(httpResponse, httpClient, httpGet);
} finally {
// 释放资源
release(httpResponse, httpClient);
}
}
/**
-
发送post请求;不带请求头和请求参数
-
@param url 请求地址
-
@return
-
@throws Exception
*/
public static HttpClientResult doPost(String url) throws Exception {
return doPost(url, null, null);
}
/**
-
发送post请求;带请求参数
-
@param url 请求地址
-
@param params 参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doPost(String url, Map<String, String> params) throws Exception {
return doPost(url, null, params);
}
/**
-
发送post请求;带请求头和请求参数
-
@param url 请求地址
-
@param headers 请求头集合
-
@param params 请求参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建http对象
HttpPost httpPost = new HttpPost(url);
/**
-
setConnectTimeout:设置连接超时时间,单位毫秒。
-
setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
-
超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
-
setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig);
// 设置请求头
/*httpPost.setHeader(“Cookie”, “”);
httpPost.setHeader(“Connection”, “keep-alive”);
httpPost.setHeader(“Accept”, “application/json”);
httpPost.setHeader(“Accept-Language”, “zh-CN,zh;q=0.9”);
httpPost.setHeader(“Accept-Encoding”, “gzip, deflate, br”);
httpPost.setHeader(“User-Agent”, “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36”);*/
packageHeader(headers, httpPost);
// 封装请求参数
packageParam(params, httpPost);
// 创建httpResponse对象
CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果
return getHttpClientResult(httpResponse, httpClient, httpPost);
} finally {
// 释放资源
release(httpResponse, httpClient);
}
}
/**
-
发送put请求;不带请求参数
-
@param url 请求地址
-
@param params 参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doPut(String url) throws Exception {
return doPut(url);
}
/**
-
发送put请求;带请求参数
-
@param url 请求地址
-
@param params 参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doPut(String url, Map<String, String> params) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPut.setConfig(requestConfig);
packageParam(params, httpPut);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse, httpClient, httpPut);
} finally {
release(httpResponse, httpClient);
}
}
/**
-
发送delete请求;不带请求参数
-
@param url 请求地址
-
@param params 参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doDelete(String url) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpDelete.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse, httpClient, httpDelete);
} finally {
release(httpResponse, httpClient);
}
}
/**
-
发送delete请求;带请求参数
-
@param url 请求地址
-
@param params 参数集合
-
@return
-
@throws Exception
*/
public static HttpClientResult doDelete(String url, Map<String, String> params) throws Exception {
if (params == null) {
params = new HashMap<String, String>();
}
params.put(“_method”, “delete”);
return doPost(url, params);
}
/**
-
Description: 封装请求头
-
@param params
-
@param httpMethod
*/
public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
// 封装请求头
if (params != null) {
Set<Entry<String, String>> entrySet = params.entrySet();
for (Entry<String, String> entry : entrySet) {
// 设置到请求头到HttpRequestBase对象中
httpMethod.setHeader(entry.getKey(), entry.getValue());
}
}
}
/**
-
Description: 封装请求参数
-
@param params
-
@param httpMethod
-
@throws UnsupportedEncodingException
*/
public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
throws UnsupportedEncodingException {
// 封装请求参数
if (params != null) {
List nvps = new ArrayList();
Set<Entry<String, String>> entrySet = params.entrySet();
for (Entry<String, String> entry : entrySet) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
// 设置到请求的http对象中
httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
}
}
/**
-
Description: 获得响应结果
-
@param httpResponse
-
@param httpClient
-
@param httpMethod
-
@return
-
@throws Exception
*/
public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
// 执行请求
httpResponse = httpClient.execute(httpMethod);
// 获取返回结果
if (httpResponse != null && httpResponse.getStatusLine() != null) {
String content = “”;
if (httpResponse.getEntity() != null) {
content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
}
return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
}
return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
/**
-
Description: 释放资源
-
@param httpResponse
-
@param httpClient
-
@throws IOException
*/
public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
// 释放资源
if (httpResponse != null) {
httpResponse.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
6.6 启动spring-boot,测试get、post请求
/**
-
Description: HttpClientUtils工具类测试
-
@author JourWon
-
@date Created on 2018年4月19日
*/
public class HttpClientUtilsTest {
/**
-
Description: 测试get无参请求
-
@throws Exception
*/
@Test
public void testGet() throws Exception {
HttpClientResult result = HttpClientUtils.doGet(“http://127.0.0.1:8080/hello/get”);
System.out.println(result);
}
/**
-
Description: 测试get带参请求
-
@throws Exception
*/
@Test
public void testGetWithParam() throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put(“message”, “helloworld”);
HttpClientResult result = HttpClientUtils.doGet(“http://127.0.0.1:8080/hello/getWithParam”, params);
System.out.println(result);
}
/**
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
三个工作日收到了offer,头条面试体验还是很棒的,这次的头条面试好像每面技术都问了我算法,然后就是中间件、MySQL、Redis、Kafka、网络等等。
- 第一个是算法
关于算法,我觉得最好的是刷题,作死的刷的,多做多练习,加上自己的理解,还是比较容易拿下的。
而且,我貌似是将《算法刷题LeetCode中文版》、《算法的乐趣》大概都过了一遍,尤其是这本
《算法刷题LeetCode中文版》总共有15个章节:编程技巧、线性表、字符串、栈和队列、树、排序、查找、暴力枚举法、广度优先搜索、深度优先搜索、分治法、贪心法、动态规划、图、细节实现题
《算法的乐趣》共有23个章节:
- 第二个是Redis、MySQL、kafka(给大家看下我都有哪些复习笔记)
基本上都是面试真题解析、笔记和学习大纲图,感觉复习也就需要这些吧(个人意见)
- 第三个是网络(给大家看一本我之前得到的《JAVA核心知识整理》包括30个章节分类,这本283页的JAVA核心知识整理还是很不错的,一次性总结了30个分享的大知识点)
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
总结
三个工作日收到了offer,头条面试体验还是很棒的,这次的头条面试好像每面技术都问了我算法,然后就是中间件、MySQL、Redis、Kafka、网络等等。
- 第一个是算法
关于算法,我觉得最好的是刷题,作死的刷的,多做多练习,加上自己的理解,还是比较容易拿下的。
而且,我貌似是将《算法刷题LeetCode中文版》、《算法的乐趣》大概都过了一遍,尤其是这本
《算法刷题LeetCode中文版》总共有15个章节:编程技巧、线性表、字符串、栈和队列、树、排序、查找、暴力枚举法、广度优先搜索、深度优先搜索、分治法、贪心法、动态规划、图、细节实现题
[外链图片转存中…(img-21HvKIMm-1713497739803)]
《算法的乐趣》共有23个章节:
[外链图片转存中…(img-S3eaLYcn-1713497739805)]
[外链图片转存中…(img-NiWZsDqW-1713497739807)]
- 第二个是Redis、MySQL、kafka(给大家看下我都有哪些复习笔记)
基本上都是面试真题解析、笔记和学习大纲图,感觉复习也就需要这些吧(个人意见)
[外链图片转存中…(img-qxJeFBqX-1713497739809)]
- 第三个是网络(给大家看一本我之前得到的《JAVA核心知识整理》包括30个章节分类,这本283页的JAVA核心知识整理还是很不错的,一次性总结了30个分享的大知识点)
[外链图片转存中…(img-WmrkiYyf-1713497739811)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!