Differences between httpurlconnection and httpclient/performance test comparison

Source: Internet
Author: User
Ttpclient is an open-source framework that encapsulates HTTP request headers, parameters, content bodies, and responses,

Httpurlconnection is a standard Java class with nothing encapsulated. it is inconvenient to use it in Taiyuan.

Httpclient is actually an encapsulation of Java methods,

Input/output stream operations in httpurlconnection,

This interface is encapsulated into httppost (httpget) and httpresponse,

This reduces the complexity of operations. Performance test: httpurlconnection vs httpclient performance test

Version: httpurlconnection jdk1.6; httpclient 3.0.1

There is a very small application in the project. During the selection, we did a test. Before that, we will describe the two classes:

Httpurlconnection java standard class (java.net)

Httpclient Jakarta commons httpclient provides encapsulation of HTTP protocol access, including HTTP request headers, parameters, content bodies, responses, and other multi-threaded applications.

 

Test code:

Java code
  1. Import java. Io. bufferedreader;
  2. Import java. Io. ioexception;
  3. Import java. Io. inputstream;
  4. Import java. Io. inputstreamreader;
  5. Import java.net. httpurlconnection;
  6. Import java.net. malformedurlexception;
  7. Import java.net. url;
  8. Import org. Apache. commons. httpclient. defaulthttpmethodretryhandler;
  9. Import org. Apache. commons. httpclient. httpclient;
  10. Import org. Apache. commons. httpclient. httpexception;
  11. Import org. Apache. commons. httpclient. httpstatus;
  12. Import org. Apache. commons. httpclient. Methods. getmethod;
  13. Import org. Apache. commons. httpclient. Params. httpmethodparams;
  14. Public class httpclienttest {
  15. Private Static string link = "http://www.baidu.com ";
  16. Public static void main (string [] ARGs ){
  17. Long A = system. currenttimemillis ();
  18. Usehttpurlconnection ();
  19. Long B = system. currenttimemillis ();
  20. System. Out. println ("use httpurlconnection:" + (B-));
  21. Long c = system. currenttimemillis ();
  22. Usehttpclient ();
  23. Long d = system. currenttimemillis ();
  24. System. Out. println ("use httpclient:" + (D-C ));
  25. }
  26. Public static void usehttpurlconnection (){
  27. Httpurlconnection conn = NULL;
  28. URL url = NULL;
  29. String result = "";
  30. Try {
  31. Url = new java.net. URL (Link );
  32. Conn = (httpurlconnection) URL. openconnection ();
  33. Conn. setconnecttimeout (10000 );
  34. Conn. Connect ();
  35. Inputstream urlstream = conn. getinputstream ();
  36. Bufferedreader reader = new bufferedreader (New inputstreamreader (urlstream ));
  37. String S = "";
  38. While (S = reader. Readline ())! = NULL ){
  39. Result + = s;
  40. }
  41. System. Out. println (result );
  42. Reader. Close ();
  43. Urlstream. Close ();
  44. Conn. Disconnect ();
  45. } Catch (malformedurlexception e ){
  46. E. printstacktrace ();
  47. } Catch (ioexception e ){
  48. E. printstacktrace ();
  49. } Catch (exception e ){
  50. E. printstacktrace ();
  51. }
  52. }
  53. Public static void usehttpclient (){
  54. Httpclient client = new httpclient ();
  55. Getmethod method = new getmethod (Link );
  56. Method. getparams (). setparameter (httpmethodparams. retry_handler,
  57. New defaulthttpmethodretryhandler (3, false ));
  58. Try {
  59. Int statuscode = client.exe cutemethod (method );
  60. If (statuscode! = Httpstatus. SC _ OK ){
  61. System. Err. println ("method failed:" + method. getstatusline ());
  62. }
  63. Byte [] responsebody = method. getresponsebody ();
  64. System. Out. println (new string (responsebody ));
  65. } Catch (httpexception e ){
  66. System. Err. println ("Fatal protocol violation:" + E. getmessage ());
  67. E. printstacktrace ();
  68. } Catch (ioexception e ){
  69. System. Err. println ("Fatal Transport Error:" + E. getmessage ());
  70. E. printstacktrace ();
  71. } Finally {
  72. Method. releaseconnection ();
  73. }
  74. }
  75. }

 

Test results:

Use httpurlconnection: 47

Use httpclient: 641

 

The results are clearly displayed, but in actual application, we should make a trade-off based on actual needs.

Related Articles:

Comparison between httpurlconnection and httpclient and use rules

Differences between httpurlconnection and httpclient/performance test comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.