HttpClient相关的工具类。
常用的两个方法:
Type | Name and description |
---|---|
static byte[] |
bodyAsBytes(org.apache.http.HttpResponse resp) 将HttpResponse的body转成byte数组 |
static void |
bodyAsFile(org.apache.http.HttpResponse resp, String filename) 将HttpResponse的body保存成文件 |
static String |
bodyAsString(org.apache.http.HttpResponse resp, String charset = null) 将HttpResponse的body转成字符串 |
static void |
bodyToFile(org.apache.http.HttpResponse resp, String filename) |
static org.apache.http.client.HttpClient |
createClient(Integer socketTimeout = 30000, Integer connTimeout = 3000) 创建一个默认的http client |
static org.apache.http.protocol.HttpContext |
createContext(Map cookies = null) 创建一个上下文,以保持同一个session中进行请求 -- 管理用户/线程状态 |
static List<org.apache.http.NameValuePair> |
createParams(Map map) 从map创建post需要的参数 |
static Object |
get(String url, Map options = [:] ) GET远程服务器的资源,并返回结果 |
static org.apache.http.HttpResponse |
get(String url, Map args, org.apache.http.client.HttpClient client, org.apache.http.protocol.HttpContext context, boolean followRedirects) 远程get数据 |
static String |
getCharset(org.apache.http.HttpResponse resp, String defaultCharset = "ISO-8859-1") 根据HttpResponse获得返回内容的字符集 |
static String |
getContentType(org.apache.http.HttpResponse resp, String defaultContent = "text/html") 根据HttpResponse获得返回内容的content type |
static Map |
getCookies(org.apache.http.protocol.HttpContext context) 从context的CookieStore中取出所有Cookie |
static void |
initCommonMethodOptions(Map options) 初始化get/post方法中常用的http参数 |
static Object |
post(String url, Map params, Map options = [:] ) Post数据到远程服务器,并返回结果 |
static org.apache.http.HttpResponse |
post(String url, Map args, org.apache.http.client.HttpClient client, org.apache.http.protocol.HttpContext context, boolean followRedirects, String charsetParams) Post数据 |
static void |
renderImage(org.apache.http.HttpResponse respSource, javax.servlet.http.HttpServletResponse respTarget) 根据HttpClient的Response,输出到当前的response中 |
static void |
setCookies(org.apache.http.protocol.HttpContext context, Map cookies) 发送请求前,设置Cookie |
static void |
setHeaders(org.apache.http.client.methods.HttpRequestBase method, Map headers) 发送请求前,设置Http头 |
static void |
setProxy(org.apache.http.client.HttpClient client, Map proxy) 设置代理服务器 |
Methods inherited from class | Name |
---|---|
class Object |
Object#wait(long), Object#wait(long, int), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
将HttpResponse的body转成byte数组
resp
- HttpResponse对象将HttpResponse的body保存成文件
resp
- HttpResponse对象filename
- 文件名将HttpResponse的body转成字符串
resp
- HttpResponse对象charset
- 字符集创建一个默认的http client
socketTimeout
- 远程应用的响应超时connTimeout
- 网络连接超时创建一个上下文,以保持同一个session中进行请求 -- 管理用户/线程状态
cookies
- Cookie列表,或者仅包含键值对的Map从map创建post需要的参数
map
- Map形式的参数GET远程服务器的资源,并返回结果
url
- HTTP URL地址options
- 其他参数,包括
网络参数: .默认为: [connectionTimeout:3000, socketTimeout:30000, followRedirects:true] request参数: .cookies: Cookie Map,其key为cookie名,value为值。 .headers: Header Map,其key为http头名,value为值。 .proxy : 代理服务器,包括host、port、username、password四个参数,其中后两个参数可为空 response参数: .charset: 远程服务器返回结果的字符集,默认自适应。 .saveFile: 本地文件名。请求完成后,将结果保存到本地文件中。 返回类型参数: .type: 0/null(默认)返回字符串,1返回一个Map,包含[response:HttpResponse, context:HttpContext, statusCode:Integer]
远程get数据
url
- HTTP URL地址args
- 请求的参数Map,包含: [headers:请求的Http头Map]client
- HttpClient对象context
- HttpContext对象followRedirects
- 是否跟随重定向根据HttpResponse获得返回内容的字符集
defaultCharset
- 当header里找不到Content-Type里的字符集时,返回的默认字符集根据HttpResponse获得返回内容的content type
defaultContent
- 当header里找不到Content-Type时,返回的默认类型从context的CookieStore中取出所有Cookie
初始化get/post方法中常用的http参数
Post数据到远程服务器,并返回结果
url
- HTTP URL地址params
- 提交参数的Map,如 [id:123, name:abc, checked:[1,2,3]]options
- 其他参数
网络参数: . 默认为 [connectionTimeout:3000, socketTimeout:30000, followRedirects:true]。 request相关参数: . charsetParams:提交的数据(params、string)的字符集,默认为UTF-8。 . files: 附件上传的Map,其key为文件名、value为文件的全路径。 . string: 直接提交一个字符串(如一个XML),并用参数charsetParams控制其编码。 . cookies: Cookie Map,其key为cookie名,value为值。 . headers: Header Map,其key为http头名,value为值。 . proxy: 代理服务器,包括host、port、username、password四个参数,其中后两个参数可为空 response相关参数: . charset: 远程服务器返回结果的字符集,默认自适应。 . saveFile: 本地文件名。请求完成后,将结果保存到本地文件中。 返回类型: . type: 0/null(默认)返回字符串,1返回一个Map,包含[response:HttpResponse, context:HttpContext, statusCode:Integer]
Post数据
url
- HTTP URL地址args
- 请求的参数Map,包含:[params:参数Map,string:直接提交的字符串, files:[上传的附件名称:文件全路径...], headers:请求的Http头Map],其中string会覆盖params、files。client
- HttpClient对象context
- HttpContext对象,包含cookiefollowRedirects
- 是否跟随重定向encodeCharset
- Post的参数编码集,默认为UTF-8根据HttpClient的Response,输出到当前的response中
发送请求前,设置Cookie
发送请求前,设置Http头
设置代理服务器
client
- HttpClient对象proxy
- 代理服务器,包括host、port、username、password四个参数,其中后两个参数可为空