修复GET较长字符串数据时数据流中断的bug

This commit is contained in:
2022-02-07 20:54:42 +08:00
parent ddf4b4bc07
commit e91812f942
4 changed files with 190 additions and 150 deletions

View File

@@ -0,0 +1,51 @@
package com.hxuanyu.starter.test;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class OtherTest {
private static final String STATION_NAME_URL = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9226";
@Test
public void testHttpClient() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet(STATION_NAME_URL);
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -5,17 +5,11 @@ import com.hxuanyu.network.service.HttpService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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)
public class NetworkTest {
@@ -24,18 +18,11 @@ 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 {
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);
public void testGet(){
Msg<String> msg = httpService.doGet("https://baidu.com");
logger.info("GET测试{}", msg);
assert msg.isSuccess();
}