最近因为项目的要求,需要使用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。

使用httpclient发送post请求与get请求的更多相关文章

  1. 使用httpclient发送get或post请求

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  2. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  3. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL

    这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...

  6. 使用HttpClient发送请求、接收响应

    使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象.  CloseableHttpClient httpclient = HttpClients.c ...

  7. HttpClient发送get post请求和数据解析

    最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写,我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要传 ...

  8. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  9. (三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    文章来源:http://www.cnblogs.com/smyhvae/p/4006009.html 一.GET和POST的对比: 在漫长的时间当中,其他的方法逐渐的退出了历史舞台,最常用的只剩下GE ...

  10. httpClient 发送请求后解析流重用的问题(HttpEntity的重用:BufferedHttpEntity)

    使用场景: 项目中使用httpClient发送一次http请求,以流的方式处理返回结果,开始发现返回的流只能使用一次,再次使用就会出错,后来看了一些解决方案,EntityUtils.consume(r ...

随机推荐

  1. 关于MVC中DropDownListFor的一个bug

    如以下代码: //后台 代码 ViewData["source_type"] = new List<SelectListItem> { "}, "} ...

  2. MVC4 EF6 MYSQL

    在MVC的框架下连接mysql数据库 将EF框架升级到EF6 将NEW JSON升级到与之相匹配的版本 然后进行相应的配置就可以了

  3. c# 自己制作一个简单的项目倒计时器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. [Angular 2] More on *ngFor, @ContentChildren & QueryList<>

    In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...

  5. php生成CSV格式(转)

    参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...

  6. fl,flash,mx包的区别

    在ActionScript项目中还真是不能使用mx包中的UI组件.           Adobe官方论坛上有一个帖子讲述了这个问题,大致意思是说:你要使用mx包中像Button这样的UI组件都是从U ...

  7. The 7 Stages Of Scaling Web Apps--reference

    reference from:http://highscalability.com/7-stages-scaling-web-apps TUESDAY, SEPTEMBER 23, 2008 AT 4 ...

  8. __asm__ __volatile__("": : :"memory");

    参考:http://stackoverflow.com/questions/14950614/working-of-asm-volatile-memory asmvolatile("&quo ...

  9. tomcat 详解五 tomcat页面设置访问权限

    转自:http://blog.knowsky.com/191233.htm 在web应用中,对页面的访问控制通常通过程序来控制,流程为:登录 -> 设置session -> 访问受限页面时 ...

  10. 记录asp.net网站停止运行原因的代码

    记录网站是什么原因导致停止运行还是有必要的,下面是具体的实现方式. protected void Application_End(object sender, EventArgs e) { Recor ...