您的位置:首页 > 理论基础 > 计算机网络

以HttpGet方法获取URL数据并转换成指定编码格式的字符串

2013-01-28 13:34 1091 查看
/***
*
* 以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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐