网络请求工具bug修改

This commit is contained in:
hanxuanyu 2022-02-07 17:31:12 +08:00
parent 38416ff420
commit ddf4b4bc07
2 changed files with 19 additions and 35 deletions

View File

@ -108,7 +108,6 @@ public class HttpServiceImpl implements HttpService {
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params) { public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
// 创建httpClient对象 // 创建httpClient对象
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 创建访问的地址 // 创建访问的地址
URIBuilder uriBuilder = new URIBuilder(url); URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) { if (params != null) {
@ -134,14 +133,8 @@ public class HttpServiceImpl implements HttpService {
// 创建httpResponse对象 // 创建httpResponse对象
CloseableHttpResponse httpResponse = null; CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果 // 执行请求并获得响应结果
return getHttpClientResult(httpClient, httpGet); return getHttpClientResult(httpClient, httpGet);
} finally {
// 释放资源
release(httpClient);
}
} catch (Exception e) { } catch (Exception e) {
logger.error("请求过程出现异常: {}", e.getMessage()); logger.error("请求过程出现异常: {}", e.getMessage());
return Msg.failed("网络请求发生异常:" + e.getMessage()); return Msg.failed("网络请求发生异常:" + e.getMessage());
@ -248,12 +241,12 @@ public class HttpServiceImpl implements HttpService {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig); httpPost.setConfig(requestConfig);
// 设置请求头 // 设置请求头
httpPost.setHeader("Cookie", ""); // httpPost.setHeader("Cookie", "");
httpPost.setHeader("Connection", "keep-alive"); // httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Accept", "application/json"); // httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9"); // httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br"); // 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"); // 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); packageHeader(headers, httpPost);
// 封装请求参数 // 封装请求参数
@ -265,13 +258,6 @@ public class HttpServiceImpl implements HttpService {
e.printStackTrace(); e.printStackTrace();
logger.error("请求过程出现异常: {}", e.getMessage()); logger.error("请求过程出现异常: {}", e.getMessage());
return Msg.failed("请求过程出现异常: " + e.getMessage()); return Msg.failed("请求过程出现异常: " + e.getMessage());
} finally {
// 释放资源
try {
release(httpClient);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
@ -342,12 +328,6 @@ public class HttpServiceImpl implements HttpService {
e.printStackTrace(); e.printStackTrace();
logger.error("请求过程出现异常: {}", e.getMessage()); logger.error("请求过程出现异常: {}", e.getMessage());
return Msg.failed("网络请求时发生异常: " + e.getMessage()); return Msg.failed("网络请求时发生异常: " + e.getMessage());
} finally {
try {
release(httpClient);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
@ -386,12 +366,6 @@ public class HttpServiceImpl implements HttpService {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return Msg.failed("转换String时发生IO异常"); return Msg.failed("转换String时发生IO异常");
} finally {
try {
release(httpClient);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }

View File

@ -12,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import java.util.HashMap;
import java.util.Map;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class) @SpringBootTest(classes = MainApplication.class)
@ -21,11 +24,18 @@ public class NetworkTest {
HttpService httpService; HttpService httpService;
private final Logger logger = LoggerFactory.getLogger(NetworkTest.class); private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
private static final String STATION_NAME_URL = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9226";
@Test @Test
@Timeout(3000) @Timeout(3000)
public void testGet() throws InterruptedException { public void testGet() throws InterruptedException {
Msg<String> msg = httpService.doGet("https://baidu.com"); Map<String, String> header = new HashMap<>(1);
// header.put("Accept", "*/*");
header.put("Connection", "keep-alive");
// header.put("User-Agent", "PostmanRuntime/7.29.0");
// header.put("Accept-Encoding", "gzip, deflate, br");
Msg<String> msg = httpService.doGet(STATION_NAME_URL, header, null);
logger.info("GET测试{}", msg); logger.info("GET测试{}", msg);
assert msg.isSuccess(); assert msg.isSuccess();
} }