Recently, because of the requirements of the project, you need to use HttpClient to send the request. But many blogs have been consulted, and the methods of sending requests vary. The reason is that because of the different versions of the HttpClient jar package, the internal methods are not the same. Therefore, it makes no sense to throw away the jar packages that are used in a straightforward way, and it is easy to find a way out. So here is the jar package used, and the method that is called under this jar package.
To send a POST request:
@Controllerpublic class Postcontroller {@RequestMapping (value= "request.html") Public Modelandview Requestpost ( HttpServletRequest request, @RequestParam (value= "code") String code) {authconfig config = authconfig.getinstance (); String Server = config.getconfigparameter ("server"); String client_id = Config.getconfigparameter ("client_id"); String Client_secret = Config.getconfigparameter ("Client_secret"); String Redirect_uri = Config.getconfigparameter ("Redirect_uri"); String scope = config.getconfigparameter ("Scope"); SYSTEM.OUT.PRINTLN ("Authorization Code is:" +code); String url = server+ "authserver/oauth2/token?client_id=" +client_id+ "&client_secret=" +client_secret+ "& Grant_type=authorization_code&code= "+code+" &redirect_uri= "+redirect_uri+" &scope= "+scope"; System.out.println (URL); HttpClient client1 = new HttpClient (); Postmethod method1 = new Postmethod (URL); Method1.setrequestheader ("Content-type", "Application/json; UTF-8 "); Method1.setrequestheader ("Content-type", "text/html; UTF-8"); namevaluepair[] param = {new Namevaluepair ("Age", "one"),//New Namevaluepair ("name", "Jay"),}; Method1.setrequestbody (param); int Statuscode=0;try {statusCode = Client1.executemethod (method1);} catch (HttpException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (StatusCode); try {System.out.println (method1.getresponsebodyasstring ())} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Method1.releaseconnection (); System.out.println ("..... ... post done ..."); return new Modelandview ("Success");}}------("").
To send a GET request:
@Controller Public classAuthorizecode {@RequestMapping (value= "Code.html") PublicModelandview Requestpost (httpservletrequest request) {HttpClient client=NewHttpClient (); GetMethod Method=NewGetMethod ("Http://localhost:8080/AuthServer/oauth2/authorize?client_id=4o7ymfOJbTNDSPPYvPqyFHvT&client_ Secret=jgjfw0l4e7ouuh09uh6olurw&redirect_uri=https://www.hao123.com&response_type=code&scope= 10216051013292419216811211005:close "); //method.getparams (). Setparameter ("client_id", "4O7YMFOJBTNDSPPYVPQYFHVT"); //method.getparams (). Setparameter ("Client_secret", "JGJFW0L4E7OUUH09UH6OLURW"); //method.getparams (). Setparameter ("Redirect_uri", "http://localhost: 8080/app/request.html "); //method.getparams (). Setparameter ("Response_type", "Code"); //method.getparams (). Setparameter ("Scope", "10216051013292419216811211005:close"); Try { //Execute the method. intStatusCode =Client.executemethod (method); System.out.println (StatusCode); if(StatusCode = =HTTPSTATUS.SC_OK) { //ins = Method.getresponsebodyasstream (); String SS =method.getresponsebodyasstring (); SYSTEM.OUT.PRINTLN (ss); } Else{System.err.println ("Response Code:" +StatusCode); } } Catch(HttpException e) {System.err.println ("Fatal protocol violation:" +e.getmessage ()); } Catch(IOException e) {System.err.println ("Fatal Transport Error:" +e.getmessage ()); } finally{method.releaseconnection (); //if (INS! = null) {//Ins.close (); // } } System.out.println ("... get done .............. ......."); return NewModelandview ("Code"); }
Using both methods in your project requires the introduction of a jar package:
Commons-codec-1.6.jar;
Commons-httpclient-3.1-osgi.jar;
Commons-logging-1.1.1.jar;
Httpcore-4.0.jar.
Send a POST request with a GET request using httpclient