我是想从外部接口那里通过流的方式拿到我想要的数据,这个url在测试工具上是可以拿得到对应数据的,但是在流里面就为null了,求解,是不是用流取这种思路不对啊,我是从这篇文章那里借鉴的https://blog.csdn.net/myme95/article/details/89359677?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7.pc_relevant_default&utm_relevant_index=14
运行结果及报错内容
你这个肯定不行啊,你要用springboot发请求,要用resttemple
public static Map<String, String> httpPost(String url, String param, Map<String, String> headers, ContentType contentType) {
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
Map<String, String> result = new HashMap<>();
try {
// 设置header
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
StringEntity paramEntity = new StringEntity(param, contentType);
httpPost.setEntity(paramEntity);
response = httpClient.execute(httpPost);
result = getResult(response);
} catch (IOException e) {
log.error("http请求异常,{}", url, e);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
log.error("response.close失败", e);
}
}
return result;
}
/**
* 获取Response返回结果
*
* @param response
* @return
* @throws IOException
*/
private static Map<String, String> getResult(CloseableHttpResponse response) throws IOException {
Map<String, String> result = new HashMap<>();
String statusCode = response.getStatusLine().getStatusCode() + "";
HttpEntity entity = response.getEntity();
if (entity != null) {
String content = EntityUtils.toString(entity, CHARSET);
result.put(CONTENT, content);
result.put(STATUSCODE, statusCode);
}
return result;
}
public static final String CONTENT = "content";
public static final String STATUSCODE = "statusCode";
private static final String CHARSET = "UTF-8";