HttpURLConnection与 HttpClient 区别/性能测试对比

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的比较,以及使用规则

posted @ 2014-09-30 10:38  jack_ou  阅读(28988)  评论(6编辑  收藏  举报