HTTP 获取网页内容 HttpURLConnection与HttpClient

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值