1 Importjava.io.IOException;2 3 ImportNet.sf.json.JSONObject;4 5 ImportOrg.apache.commons.httpclient.DefaultHttpMethodRetryHandler;6 ImportOrg.apache.commons.httpclient.Header;7 Importorg.apache.commons.httpclient.HttpClient;8 Importorg.apache.commons.httpclient.HttpException;9 ImportOrg.apache.commons.httpclient.HttpStatus;Ten ImportOrg.apache.commons.httpclient.methods.GetMethod; One ImportOrg.apache.commons.httpclient.params.HttpMethodParams; A Importorg.apache.http.HttpEntity; - ImportOrg.apache.http.HttpResponse; - ImportOrg.apache.http.client.methods.HttpPost; the Importorg.apache.http.entity.StringEntity; - Importorg.apache.http.impl.client.DefaultHttpClient; - Importorg.apache.http.util.EntityUtils; - + Public classTestgetrequest { - + /** A * @paramargs at */ - Public Static voidMain (string[] args) { - -String url = "http://www.ecmwf.int/plots/get-map/"; -String param = "{\" product\ ": {\" product\ ": {\" name\ ": \" Public_plots\ ", \" package\ ": \" medium\ "},\" values\ ": {\" time\ " : [\ "2016032012\", 48,\ "2016032212\"],\ "parameter\": \ "Wind 850 and mslp\", \ "area\": \ "Asia\"}} "; - System.out.println (param); inJsonobject Object =jsonobject.fromobject (param); -Jsonobject result =doPost (URL, object); to System.out.println (result); +Jsonobject Imgresult = (jsonobject) result.get ("Result"); -System.out.println (imgresult.get ("url")); the } * $ Public StaticString doget (string URL, string charset)throwsException {Panax Notoginseng /* - * Use GetMethod to access a URL corresponding to the page, the implementation steps: 1: Generate a Httpclinet object and set the appropriate parameters. the * 2: Generate a GetMethod object and set the parameters for the response. 3: Use Httpclinet generated object to perform GetMethod generated get + * method. 4: Process response status code. 5: If the response is normal, the HTTP response content is processed. 6: Release the connection. A */ the /*1 generating Httpclinet objects and setting parameters*/ +HttpClient HttpClient =NewHttpClient (); - //Set Http connection timeout to 5 seconds $ Httpclient.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout ( $5000); - /*2 generating GetMethod objects and setting parameters*/ -GetMethod GetMethod =Newgetmethod (URL); the //set the GET request timeout to 5 seconds -Getmethod.getparams (). Setparameter (Httpmethodparams.so_timeout, 5000);Wuyi //set request retry processing with default retry processing: request three times the getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler, - NewDefaulthttpmethodretryhandler ()); WuString response = ""; - /*3 Executing an HTTP GET request*/ About Try { $ intStatusCode =Httpclient.executemethod (GetMethod); - /*4 Determining the status code of Access*/ - if(StatusCode! =HTTPSTATUS.SC_OK) { -SYSTEM.ERR.PRINTLN ("Request Error:" +getmethod.getstatusline ()); A } + /*5 Handling HTTP Response content*/ the //HTTP response header information, simple print here -header[] headers =getmethod.getresponseheaders (); $ for(Header h:headers) the System.out the. println (H.getname () + "------------" +H.getvalue ()); the //Read HTTP response content, here simple to print Web content the byte[] Responsebody = Getmethod.getresponsebody ();//read as byte array -Response =NewString (Responsebody, CharSet); inSYSTEM.OUT.PRINTLN ("----------Response:" +response); the //read as InputStream, it is recommended to use when the amount of Web content data is large the //InputStream response = Getmethod.getresponsebodyasstream (); About}Catch(HttpException e) { the //A fatal exception may be the protocol is wrong or the content returned is problematic theSystem.out.println ("Please check the input url!"); the e.printstacktrace (); +}Catch(IOException e) { - //Network exception occurred theSYSTEM.OUT.PRINTLN ("A network exception has occurred!"));Bayi e.printstacktrace (); the}finally { the /*6. Release the connection*/ - getmethod.releaseconnection (); - } the returnresponse; the } the the Public Staticjsonobject doPost (String URL, jsonobject json) { -Defaulthttpclient client =Newdefaulthttpclient (); theHttpPost post =Newhttppost (URL); theJsonobject response =NULL; the Try {94stringentity s =Newstringentity (json.tostring ()); theS.setcontentencoding ("UTF-8"); theS.setcontenttype ("Application/json");//sending JSON data requires setting ContentType the post.setentity (s);98HttpResponse res =Client.execute (POST); About if(Res.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { -httpentity entity =res.getentity ();101String result = entityutils.tostring (res.getentity ());//returns the JSON format:102Response =jsonobject.fromobject (result);103 }104}Catch(Exception e) { the Throw NewRuntimeException (e);106 }107 returnresponse;108 }109 the}
Java_get Request URL