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;
}