Compared to HttpURLConnection, httpclient richer, and more powerful, of which Apache has two items are httpclient, one is the commonts package, this is universal, More professional is the org.apache.http, so I generally use the latter;
HttpClient can handle long connections, save sessions, reconnect, and request filters, connection reuse, etc...
Here is the test code (all summarized from the official documentation, as well as the translation)
Need to download Core package: Httpclient-4.3.4.jar, can also be downloaded on the official website: http://hc.apache.org/downloads.cgi
--------------------------Fast API---------------------------------/** * Fast API provides only the most basic functionality for scenes that do not require flexible expansion */private static void Test22 () throws clientprotocolexception, ioexception{string result = request.get ("/HTTP/ Webservice.webxml.com.cn/webservices/mobilecodews.asmx/getdatabaseinfo "). ConnectTimeout (1000)// Set the server request timeout. Sockettimeout (1000)//Set the server to time out. Execute (). Returncontent (). asstring (); String result2 = Request.post ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo"). Useexpectcontinue (). Version (Httpversion.http_1_1). bodystring ("Parameters", Contenttype.default_text). Execute (). Returncontent (). asstring ();//submit an HTML form and save the returned result Request.post ("http://webservice.webxml.com.cn/WebServices/ Mobilecodews.asmx/getdatabaseinfo "). AddHeader (" X-custom-header "," stuff ")//The form header. Viaproxy (New Httphost (" Myproxy ", 8080))//Set agent. BodyForm (Form.form ()//forms. Add (" Mobilecode "," 12345 "). Add (" UserID ", "123456"). Build ()). Execute () . Savecontent (New File ("Result.txt")); SYSTEM.OUT.PRINTLN (result); System.out.println (RESULT2);} /** * rapid development with executor; * If you need to perform certain requests in a specified security context, we can also use exector directly, * This time the user's authentication information will be cached for subsequent requests to use. */private static void test23 () throws Clientprotocolexception, ioexception{Executor Executor = executor.newinstance () . Auth (New Httphost ("Somehost", 8080), "username", "password")//Add authentication. Authpreemptive (New Httphost ("s Omehost ", 8080)); Use the Preemptive authentication Executor.execute (Request.get ("http://somehost/"))//To perform a Get request. Returncontent (). asstring (); Executor.execute (Request.post ("http://somehost/")//executes the Post request. Useexpectcontinue (). bodystring ("Imp Ortant stuff ", Contenttype.default_text)). Returncontent (). asstring (); }/** * * FAST response processing * * Quickly send a GET request with request and return the result with the ResponseHandler callback; */private static void Test24 () throws Clientprotocolexc Eption, IOException {Object result = Request. Get ("Http://webservice.webxml.com.cn/WebSerVices/mobilecodews.asmx/getdatabaseinfo "). Execute (). Handleresponse (New responsehandler<object> () {public Object handleresponse (final HttpResponse response) throws IOException {Statusline statusline = Response.getstatusline () if (Statusline.getstatuscode () ==200) {httpentity entity = response.getentity (); if (entity! = NULL) {String str = Entityutils.tostring (entity); return str;}} return null;}}); if (result! = null) {System.out.println (">>>>>>" +result);}}