网络请求工具bug修改
This commit is contained in:
parent
38416ff420
commit
ddf4b4bc07
@ -108,7 +108,6 @@ public class HttpServiceImpl implements HttpService {
|
||||
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
|
||||
// 创建httpClient对象
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
|
||||
// 创建访问的地址
|
||||
URIBuilder uriBuilder = new URIBuilder(url);
|
||||
if (params != null) {
|
||||
@ -134,14 +133,8 @@ public class HttpServiceImpl implements HttpService {
|
||||
|
||||
// 创建httpResponse对象
|
||||
CloseableHttpResponse httpResponse = null;
|
||||
|
||||
try {
|
||||
// 执行请求并获得响应结果
|
||||
return getHttpClientResult(httpClient, httpGet);
|
||||
} finally {
|
||||
// 释放资源
|
||||
release(httpClient);
|
||||
}
|
||||
// 执行请求并获得响应结果
|
||||
return getHttpClientResult(httpClient, httpGet);
|
||||
} catch (Exception e) {
|
||||
logger.error("请求过程出现异常: {}", 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();
|
||||
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");
|
||||
// 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);
|
||||
|
||||
// 封装请求参数
|
||||
@ -265,13 +258,6 @@ public class HttpServiceImpl implements HttpService {
|
||||
e.printStackTrace();
|
||||
logger.error("请求过程出现异常: {}", 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();
|
||||
logger.error("请求过程出现异常: {}", 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) {
|
||||
e.printStackTrace();
|
||||
return Msg.failed("转换String时发生IO异常");
|
||||
} finally {
|
||||
try {
|
||||
release(httpClient);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
@ -21,11 +24,18 @@ public class NetworkTest {
|
||||
HttpService httpService;
|
||||
|
||||
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
|
||||
@Timeout(3000)
|
||||
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);
|
||||
assert msg.isSuccess();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user