/***
*
* 以HttpGet方法获取URL数据并转换成指定编码格式的字符串
* @param url url
* @param charset 字符编码,默认为UTF-8编码
* @return
* @throws ParseException
* @throws ClientProtocolException
* @throws IOException
*/
public String getStringContent(String url, String charset) throws ParseException, ClientProtocolException, IOException{
if( charset == null || charset.equals("") ){
return EntityUtils.toString( getHttpEntity( url ), HTTP.UTF_8 );
} else {
return EntityUtils.toString( getHttpEntity( url ), charset);
}
}
public static HttpEntity getHttpEntity(String url) throws ClientProtocolException, IOException {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 3000);
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpClient client = new DefaultHttpClient(params);
HttpGet get = new HttpGet(url);
HttpResponse httpResponse = client.execute(get);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
return httpResponse.getEntity();
}
return null;
}
以HttpGet方法获取URL数据并转换成指定编码格式的字符串
最新推荐文章于 2023-01-11 11:37:58 发布