HTTP 获取网页内容 HttpURLConnection与HttpClient

本文对比了使用java.net.HttpURLConnection和Apache HttpClient两种方法获取网页内容的实现方式、优势及区别,详细阐述了如何利用这些类进行HTTP请求,并解析响应内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/fd.php?i=985641568515307&s=219aef95897911f518f8040247ec08de

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值