在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
2018-01-25 16:33
1896 查看
HttpClient中的请求,一个方法中只能请求一次。
解决方案:
在springBoot中使用restTemplate的方法:
工具类:
解决方案:
在springBoot中使用restTemplate的方法:
工具类:
/** * @author xwolf * @since 1.8 **/ public class RestUtil { /** * get 请求 * @param url 请求的url * @param map 参数 * @param clazz 返回结果类型 * @param <T> * @return */ public static <T> T get(String url, Map<String,Object> map,Class<T> clazz){ RestTemplate restTemplate = new RestTemplate(); ResponseEntity<T> responseEntity = restTemplate.getForEntity(url,clazz,map); return responseEntity.getBody(); } /** * <code>https://www.programcreek.com/java-api-examples/index.php?class=org.springframework.web.client.RestTemplate&method=postForEntity</code> * @param url 请求的结果 * @param map 请求参数 * @param clazz 返回结果类型 * @param <T> * @return */ public static <T> T post(String url, MultiValueMap<String,Object> map, Class<T> clazz){ RestTemplate restTemplate = new RestTemplate(); ResponseEntity<T> responseEntity = restTemplate.postForEntity(url,map,clazz); return responseEntity.getBody(); } /** * 各種請求 * <code>https://www.programcreek.com/java-api-examples/index.php?class=org.springframework.web.client.RestTemplate&method=exchange</code> * <example> * HttpHeaders headers = new HttpHeaders(); * headers.add("access","token"); * MultiValueMap<String,Object> map = new LinkedMultiValueMap<>(); * map.add("username","rwrwer"); * HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(map,headers); * String content = RestUtil.exchange(url, HttpMethod.POST,entity,String.class); * </example> * @param url * @param httpMethod * @param httpEntity * @param clazz * @param <T> * @return */ public static <T> T exchange(String url, HttpMethod httpMethod,HttpEntity<?> httpEntity,Class<T> clazz){ RestTemplate restTemplate = new RestTemplate(); ResponseEntity<T> responseEntity = restTemplate.exchange(url,httpMethod,httpEntity,clazz); return responseEntity.getBody(); } }
相关文章推荐
- 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
- 【问题解决】HttpClient解析服务器返回的response出现乱码
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
- android 解决weixin微信jar包在打包的时候出现的java.io.IOException: Can't read问题
- apache-karaf-3.0.0发布features,出现"java.io.IOException: Error resolving artifact"异常解决
- 解决发http get请求的时候不成功,出现android.os.NetworkOnMainThreadException的异常
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
- 使用Dubbo服务出现java.io.IOException: invalid constant type: 18异常解决办法
- Xutil过时出现的HttpException:java.io,IoException:stack size 1036KB的异常
- javaIE下载文件中取消下载出现ClientAbortException: java.io.IOException异常
- Java httpclient请求,解决乱码问题
- 关于solr异常:org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http:192.168.0.11/solr/的解决
- android环境下解决java.io.IOException: Malformed ipv6异常的方法
- Myeclipse(Tomcat)发布工程出现java.io.EOFException异常的原因和解决方法
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- java.io.IOException: Server returned HTTP response code: 403 for URL: http://的解决办法
- JavaWeb -- http-equiv=refresh跳转的时候出现Session 丢失, 解决办法。。
- java中用httpClient请求service时bug解决方法(httpGet)
- IOException while loading persisted sessions: java.io.EOFException异常的解决办法
- Tomcat:IOException while loading persisted sessions: java.io.EOFException解决Tomcat启动异常