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
- 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-));
- 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.exe cutemethod (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 ();
- }
- }
- }
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