使用httpclient发送post请求与get请求
最近因为项目的要求,需要使用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请求的更多相关文章
- 使用httpclient发送get或post请求
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...
- 使用HttpClient发送请求、接收响应
使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象. CloseableHttpClient httpclient = HttpClients.c ...
- HttpClient发送get post请求和数据解析
最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写,我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要传 ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- (三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
文章来源:http://www.cnblogs.com/smyhvae/p/4006009.html 一.GET和POST的对比: 在漫长的时间当中,其他的方法逐渐的退出了历史舞台,最常用的只剩下GE ...
- httpClient 发送请求后解析流重用的问题(HttpEntity的重用:BufferedHttpEntity)
使用场景: 项目中使用httpClient发送一次http请求,以流的方式处理返回结果,开始发现返回的流只能使用一次,再次使用就会出错,后来看了一些解决方案,EntityUtils.consume(r ...
随机推荐
- 批量下载QQ空间日志
从手机页面读取,有时候也会卡死,解决办法还是重新来……………… # -*-coding:utf-8-*- # 作者:fwindpeak # import urllib import urllib2 i ...
- zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap
Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- leetcode || 53、Maximum Subarray
problem: Find the contiguous subarray within an array (containing at least one number) which has the ...
- DB9 公头母头引脚定义及连接
1.实物及引脚简单介绍 在做开发的时候常常会用到串行接口,一般9针的串行接口居多.例如以下图所看到的: 公头母头用于连接线的採用上图封装.但用于开发板的时候採用90度弯角插针的封装.例如以下图: 各引 ...
- Android源码编译的全过程记录
写本篇文章主要参考了官方文档和网上的一些资料,但是对于Android最新的代码来说,网上资料有些已经过时.本文中步骤已经作者实验,大家可以亲自执行试试.由于没有使用Eclipse的习惯,所以没有做Ec ...
- MySQL 列子查询及 IN、ANY、SOME 和 ALL 操作符的使用(转)
MySQL 列子查询 列子查询是指子查询返回的结果集是 N 行一列,该结果通常来自对表的某个字段查询返回. 一个列子查询的例子如下: SELECT * FROM article WHERE uid I ...
- MySQL中的WITH ROLLUP
MySQL中的WITH ROLLUP MySQL的扩展SQL中有一个非常有意思的应用WITH ROLLUP,在分组的统计数据的基础上再进行相同的统计(SUM,AVG,COUNT…),非常类似于Orac ...
- PHP.9-HTML+CSS(三)-CSS样式
CSS样式 CSS是Cascading Style Sheets的简写,它除了可以轻松设置网页元素的显示位置和格式处,甚至还能产生滤镜,图像淡化,网页淡入淡出的渐变效果.CSS就是要对网页的显示效果实 ...
- hashTable(哈希表)的基本用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- hihocoder 1237 Farthest Point
#1237 : Farthest Point 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 Given a circle on a two-dimentional pla ...