最近由于项目的要求,须要使用httpclient来发送请求。可是查阅了许多博客,你们发送请求的方法各不相同。缘由是由于httpclient的jar包的不一样版本,其内部方法也不相同。所以抛开具体用到的jar包而直接复制方法是没有意义的,很容易出现找不到方法的状况。因此在此给出用到的jar包,和在这个jar包下调用的方法。html
发送post请求:java
@Controller public 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("受权码是:"+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", "11"), // new NameValuePair("name", "jay"), }; //method1.setRequestBody(param); int statusCode=0; try { statusCode = client1.executeMethod(method1); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(statusCode); try { System.out.println(method1.getResponseBodyAsString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } method1.releaseConnection(); System.out.println("...............post Done..............."); return new ModelAndView("success"); } }
发送get请求:json
@Controller public class AuthorizeCode { @RequestMapping(value="code.html") public ModelAndView requestPost(HttpServletRequest request){ HttpClient client = new HttpClient(); GetMethod method = new GetMethod("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. int statusCode = 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 new ModelAndView("code"); }
在项目中使用这两个方法须要引入jar包:app
commons-codec-1.6.jar;post
commons-httpclient-3.1-osgi.jar;url
commons-logging-1.1.1.jar;spa
httpcore-4.0.jar。code