public static String get(String URI) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(URI); //set timeout time RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(2000) .setConnectTimeout(2000) .build(); httpget.setConfig(requestConfig); CloseableHttpResponse httpResponse = null; String result=""; try { httpResponse = httpclient.execute(httpget); int status=httpResponse.getStatusLine().getStatusCode(); if(status>=300){ return ""; } BufferedReader reader =null; ContentType contentType = ContentType.getOrDefault(httpResponse.getEntity()); if(contentType!=null && contentType.getCharset()!=null ){ reader= new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent(),contentType.getCharset())); }else{ reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent(),"GB2312")); } String inputLine; StringBuffer bufferstr = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { bufferstr.append(inputLine); } reader.close(); result=bufferstr.toString(); }catch(Exception e){ e.printStackTrace(); } finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
相关推荐
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
利用HttpClient发送HTTP请求 利用HttpClient发送HTTP请求
Ionic+Angular+Express实现前后端交互使用HttpClient发送get请求数据并加载显示示例代码
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)
解决证书错误,利用httpClient发送https请求,内容格式为XML,另外包中有sha1加密,需要的包,速速下载
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
使用HttpClient发送http请求需要的jar包。org.apache.http的jar包
封装了一些采用HttpClient发送HTTP请求的方法;httpPOST2方法HttpUtil.post(url, params)需引入jar <dependency> <groupId>...
Java 编写的使用HttpClient发送http请求工具类(解决了session无法保留问题)
NULL 博文链接:https://daoshud1.iteye.com/blog/1847378
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
这个代码的作用是使用HttpClient发送POST请求,...通过学习这个示例,你可以了解到如何使用HttpClient发送不同类型的HTTP请求(如GET、POST、PUT、DELETE等),以及如何处理响应结果(如读取响应体、解析JSON数据等)。
httpclient发送post请求
NULL 博文链接:https://javasam.iteye.com/blog/2117845
httpclient的用法,发送get请求和post请求,设置header
NULL 博文链接:https://bijian1013.iteye.com/blog/2299764
httpclient-4.5.6.jar httpclient-cache-4.5.6.jar httpclient-win-4.5.6.jar httpcore-4.4.10.jar httpmime-4.5.6.jar 提供以上5个jar包