文章出處

ttpClient是個開源框架,封裝了訪問http的請求頭,參數,內容體,響應等等,

HttpURLConnection是java的標準類,什么都沒封裝,用起來太原始,不方便

HttpClient實際上是對Java提供方法的一些封裝,

在HttpURLConnection中的輸入輸出流操作,

在這個接口中被統一封裝成了HttpPost(HttpGet)和HttpResponse,

這樣,就減少了操作的繁瑣性。

性能方面這里做一個測試:

HttpURLConnection VS HttpClient性能測試  

版本: HttpURLConnection  jdk1.6;HttpClient 3.0.1

在項目中有一個特別小的相關應用,在選擇時做了一下測試,之前先對兩個類進行下說明:

HttpURLConnection  java的標準類(java.net)

HttpClient Jakarta Commons HttpClient,提供對HTTP協議訪問的封裝,包括http的請求頭,參數,內容體,響應等及多線程的應用。

 

測試代碼:

 

    import java.io.BufferedReader;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.InputStreamReader;  
    import java.net.HttpURLConnection;  
    import java.net.MalformedURLException;  
    import java.net.URL;  
      
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  
    import org.apache.commons.httpclient.HttpClient;  
    import org.apache.commons.httpclient.HttpException;  
    import org.apache.commons.httpclient.HttpStatus;  
    import org.apache.commons.httpclient.methods.GetMethod;  
    import org.apache.commons.httpclient.params.HttpMethodParams;  
      
    public class HttpClientTest {  
      
        private static String link = "http://www.baidu.com";  
      
        public static void main(String[] args) {  
            long a = System.currentTimeMillis();  
            useHttpURlConnection();  
            long b = System.currentTimeMillis();  
            System.out.println("use httpurlconnection: "+(b-a));  
            long c = System.currentTimeMillis();  
            useHttpClient();  
            long d = System.currentTimeMillis();  
            System.out.println("use httpclient: "+(d-c));  
        }  
          
        public static void useHttpURlConnection(){  
            HttpURLConnection conn = null;  
            URL url = null;  
            String result = "";  
            try {  
                url = new java.net.URL(link);  
                conn = (HttpURLConnection) url.openConnection();  
                conn.setConnectTimeout(10000);  
                conn.connect();  
      
                InputStream urlStream = conn.getInputStream();  
                BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream));  
                String s = "";  
                while ((s = reader.readLine()) != null) {  
                    result += s;  
                }  
                System.out.println(result);  
                reader.close();  
                urlStream.close();  
                conn.disconnect();  
            } catch (MalformedURLException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch(Exception e){  
                e.printStackTrace();  
            }  
        }  
      
        public static void useHttpClient(){  
            HttpClient client = new HttpClient();  
            GetMethod method = new GetMethod(link);  
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
                    new DefaultHttpMethodRetryHandler(3, false));  
            try {  
                int statusCode = client.executeMethod(method);  
      
                if (statusCode != HttpStatus.SC_OK) {  
                    System.err.println("Method failed: " + method.getStatusLine());  
                }  
                byte[] responseBody = method.getResponseBody();  
                System.out.println(new String(responseBody));  
            } catch (HttpException e) {  
                System.err.println("Fatal protocol violation: " + e.getMessage());  
                e.printStackTrace();  
            } catch (IOException e) {  
                System.err.println("Fatal transport error: " + e.getMessage());  
                e.printStackTrace();  
            } finally {  
                method.releaseConnection();  
            }  
        }  
    }  

 

測試結果:

use httpurlconnection: 47

use httpclient: 641

 

結果很明顯示,但是在實際應用中,還是應該根據實際的需求進行取舍。

相關文章:

HttpURLConnection和HTTPClient的比較,以及使用規則


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()