使用httpclient发送post请求与get请求

时间:2022-08-10 20:42:59

最近因为项目的要求,需要使用httpclient来发送请求。但是查阅了许多博客,大家发送请求的方法各不相同。原因是因为httpclient的jar包的不同版本,其内部方法也不相同。因此抛开具体用到的jar包而直接复制方法是没有意义的,很容易出现找不到方法的情况。所以在此给出用到的jar包,和在这个jar包下调用的方法。

发送post请求:

@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请求:

@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包:

commons-codec-1.6.jar;

commons-httpclient-3.1-osgi.jar;

commons-logging-1.1.1.jar;

httpcore-4.0.jar。