public class Request extends Object implements HeaderOperation<Request>
Constructor and Description |
---|
Request()
默认构造
|
Modifier and Type | Method and Description |
---|---|
Request |
basicAuth(String username,
String password)
简单验证,生成的头信息类似于:
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
|
HttpBody |
body()
获取请求体
|
Request |
body(HttpBody body)
添加请求体
|
Request |
body(String body)
添加字符串请求体
|
byte[] |
bodyBytes()
获取请求体字节码
|
String |
bodyStr()
获取请求体字符串
|
InputStream |
bodyStream()
获取请求体资源流
|
Charset |
charset()
获取请求编码,默认
DEFAULT_CHARSET ,一般用于:
编码请求体
服务端未返回编码时,使用此编码解码响应体
|
Request |
charset(Charset charset)
设置自定义编码,一般用于:
编码请求体
服务端未返回编码时,使用此编码解码响应体
|
Request |
form(Map<String,Object> formMap)
添加请求表单内容
|
HttpBody |
handledBody()
获取处理过的请求体,即如果是非REST的GET请求,始终返回
null |
UrlBuilder |
handledUrl()
获取处理后的请求URL,即如果为非REST的GET请求,将form类型的body拼接为URL的一部分
|
Request |
header(String name,
String value,
boolean isOverride)
设置一个header
如果覆盖模式,则替换之前的值,否则加入到值列表中 如果给定值为 null ,则删除这个头信息 |
Map<String,? extends Collection<String>> |
headers()
获取headers
|
Request |
locationTo(String location)
更新设置重定向后的URL,用于处理相对路径
注意此方法会修改对象本身 |
int |
maxRedirects()
获取最大重定向请求次数
如果次数小于1则表示不重定向,大于等于1表示打开重定向。 |
Method |
method()
获取Http请求方法
|
Request |
method(Method method)
设置请求方法
|
static Request |
of(String url)
构建一个HTTP请求,默认编解码规则,规则为:
如果传入的URL已编码,则先解码,再按照RFC3986规范重新编码。
|
static Request |
of(String url,
Charset decodeAndEncodeCharset)
构建一个HTTP请求
对于传入的URL,可以自定义是否解码已经编码的内容,规则如下: 如果url已编码,则decodeAndEncodeCharset设置为 null ,此时URL不会解码,发送也不编码。 |
static Request |
of(UrlBuilder url)
构建一个HTTP请求
|
static Request |
ofWithoutEncode(String url)
构建一个HTTP请求,不解码和编码,此时要求用户传入的URL必须是已经编码过的。
|
Response |
send()
发送请求
|
Response |
send(ClientEngine engine)
发送请求
|
Request |
setEncodeUrl(boolean isEncodeUrl)
设置是否编码URL
|
Request |
setMaxRedirects(int maxRedirects)
设置最大重定向次数
如果次数小于1则表示不重定向,大于等于1表示打开重定向 |
Request |
setRest(boolean isRest)
设置是否rest模式
rest模式下get请求不会把参数附加到URL之后,而是作为body发送 |
String |
toString() |
UrlBuilder |
url()
获取请求的URL
|
Request |
url(UrlBuilder url)
设置URL
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
auth, basicAuth, bearerAuth, contentEncoding, contentLength, contentType, cookie, cookie, cookie, disableCookie, enableDefaultCookie, header, header, header, header, header, header, header, isChunked, keepAlive, proxyAuth
public static Request ofWithoutEncode(String url)
url
- URL链接public static Request of(String url)
of(String, Charset)
指定decodeAndEncodeCharset为null
url
- URL链接,默认自动解码并重新编码URL中的参数等信息public static Request of(String url, Charset decodeAndEncodeCharset)
null
,此时URL不会解码,发送也不编码。null
,并调用setEncodeUrl(boolean)
为true编码URL。url
- URL链接decodeAndEncodeCharset
- 编码,如果为null
不自动解码编码URLpublic static Request of(UrlBuilder url)
url
- UrlBuilder
public UrlBuilder url()
public UrlBuilder handledUrl()
public Request url(UrlBuilder url)
url
- URLpublic Request locationTo(String location)
location
- 重定向后的URLpublic Request charset(Charset charset)
charset
- 编码public Charset charset()
DEFAULT_CHARSET
,一般用于:
public Request setEncodeUrl(boolean isEncodeUrl)
isEncodeUrl
- 如果为true
,则使用请求编码编码URL,false
则不编码URLpublic Map<String,? extends Collection<String>> headers()
HeaderOperation
headers
in interface HeaderOperation<Request>
public Request header(String name, String value, boolean isOverride)
null
,则删除这个头信息header
in interface HeaderOperation<Request>
name
- Header名,null
跳过value
- Header值,null
表示删除name对应的头isOverride
- 是否覆盖已有值public Request basicAuth(String username, String password)
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
username
- 用户名password
- 密码public HttpBody body()
public String bodyStr()
public byte[] bodyBytes()
public InputStream bodyStream()
public HttpBody handledBody()
null
public Request form(Map<String,Object> formMap)
formMap
- 表单内容public Request body(HttpBody body)
body
- 请求体,可以是文本、表单、流、byte[] 或 Multipartpublic int maxRedirects()
public Request setMaxRedirects(int maxRedirects)
maxRedirects
- 最大重定向次数public Request setRest(boolean isRest)
isRest
- 是否rest模式public Response send()
public Response send(ClientEngine engine)
engine
- 自自定义引擎Copyright © 2025. All rights reserved.