日期:2014-05-16 浏览次数:20428 次
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; public class ReadingWebContent { /** * * @param url * @return * @throws Exception */ public static String getContent(String url) throws Exception{ String CONTENT_CHARSET = "UTF-8"; String backContent = null; // private static final END_OBJECT = "0"; String eND_OBJECT = "0"; HttpClient httpclient = null; HttpGet httpget = null; try { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 180 * 1000); HttpConnectionParams.setSoTimeout(params, 180 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, false); httpclient = new DefaultHttpClient(params); httpget = new HttpGet(url); httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } backContent = buffer.toString(); } } catch (Exception e) { httpget.abort(); backContent = "????,???????"; System.out.println("-------------?????"); e.printStackTrace(); System.out.println("-------------??????"); }finally{ if(httpclient != null) httpclient.getConnectionManager().shutdown(