HTTP 获取网页内容 可用以下两个类实现
java.net.HttpURLConnection;
核心代码如下:
String urlString ="http://www.baidu.com/s?wd=1&rsv_bp=0&rsv_spt=3&inputT=280";
StringBuffer stringBuffer = new StringBuffer();
try {
URL url = newURL(urlString);
HttpURLConnectionurlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(Constant.TIME_OUT_INTERVAL);
urlConnection.connect();
StringcontentEncodingString = getEncoding(urlConnection);
InputStreamReaderinputStreamReader = null;
if(contentEncodingString != null)
{
inputStreamReader= new InputStreamReader(urlConnection.getInputStream(), contentEncodingString);
}else{
inputStreamReader= new InputStreamReader(urlConnection.getInputStream());
}
BufferedReaderbufReader = new BufferedReader(inputStreamReader);
StringinputLine = null;
int len = 0;
while((inputLine= bufReader.readLine()) != null)
{
stringBuffer.append(inputLine);
stringBuffer.append("\n");
}
showContentLen(stringBuffer.length());
showContent(stringBuffer.toString());
bufReader.close();
urlConnection.disconnect();
} catch (Exception e){
// TODO:handle exception
e.printStackTrace();
displayError(e.getMessage());
return ;
}
org.apache.http.client.HttpClient;
String urlString ="http://www.baidu.com/s?wd=1&rsv_bp=0&rsv_spt=3&inputT=280";
try {
HttpGethttpGet = new HttpGet(urlString);
HttpClienthttpClient = new DefaultHttpClient();
HttpResponsehttpResponse = httpClient.execute(httpGet);
HttpEntityentity = httpResponse.getEntity();
String charsetString = getContentCharSet(entity);
Log.i("", "charsetString = " + charsetString);
int responseCode= httpResponse.getStatusLine().getStatusCode();
if(responseCode == HttpStatus.SC_OK)
{
StringresultData = EntityUtils.toString(httpResponse.getEntity());
showContentLen(resultData.length());
showContent(resultData);
}else{
displayError("Getdata error!");
}
} catch (Exception e){
// TODO:handle exception
e.printStackTrace();
displayError(e.getMessage());
return ;
}
区别:
虽然java.net这个包中提供了HttpURLConnection,通过HTTP获取资源的基本功能,但是它不能够充分地满足很多应用在灵活性或功能上的要求。Jakarta工程的HttpClient组件则填补了这一空白,提供一种高效的、最新的,且功能丰富的套件的客户端,并且实现最新的HTTP标准和建议。
同时HttpClient在获取网页的时候会自动解析其编码,而HttpURLConnection则需要自己解析,如下:
publicString getEncoding(HttpURLConnection urlConnection)
{
if (urlConnection == null)
{
return null;
}
Map<String,List<String>> map = urlConnection.getHeaderFields();
Set<String> keys =map.keySet();
Iterator<String>iterator = keys.iterator();
// 遍历,查找字符编码
String key = null;
String tmp = null;
while(iterator.hasNext()) {
key = iterator.next();
tmp = map.get(key).toString().toLowerCase();
// 获取content-type charset
if (key != null && key.equals("Content-Type")) {
int m = tmp.indexOf("charset=");
if (m != -1) {
return tmp.substring(m + 8).replace("]","");
}
}
}
return null;
}
代码工程链接如下:
http://dl5.csdn.net/fd.php?i=985641568515307&s=219aef95897911f518f8040247ec08de