public final class Http extends Object
Accept属于请求头, Content-Type属于实体头
请求方的http报头结构:通用报头|请求报头|实体报头
响应方的http报头结构:通用报头|响应报头|实体报头
请求报头有:Accept、Accept-Charset、Accept-Encoding、Accept-Language、Referer、Authorization、From、Host、If-Match、User-Agent、If-Modified-Since等
Accept:告诉WEB服务器自己接受什么介质类型,*∕*表示任何类型,type∕*表示该类型下的所有子类型,type∕sub-type,如Accept(text/html)
响应报头有:Age、Server、Accept-Ranges、Vary等
实体报头有:Allow、Location、Content-Base、Content-Encoding、Content-Length、Content-Range、Content-MD5、Content-Type、Expires、Last-Modified等
Content-Type:
请求实体报头:浏览器告诉Web服务器自己发送的数据格式,如Content-Type(application/json, multipart/form-data, application/x-www-form-urlencoded)
响应实体报头:Web服务器告诉浏览器自己响应的消息格式,例如Content-Type(application/xml, application/json)
http://www.atool.org/httptest.phpRestful:https://www.cnblogs.com/pixy/p/4838268.html
http工具类
Spring RestTemplate
org.apache.httpcomponents:fluent-hc
org.apache.httpcomponents.client5:httpclient5
com.squareup.okhttp3:okhttp
| 限定符和类型 | 类和说明 |
|---|---|
static class |
Http.HttpMethod
Http method
|
| 限定符和类型 | 方法和说明 |
|---|---|
Http |
accept(ContentType contentType)
内容类型发送请求头,告诉服务器什么样的响应会接受返回
header("Accept", contentType)
|
Http |
addHeader(Map<String,String> headers)
设置请求头
|
Http |
addHeader(String name,
String value)
设置请求头
|
Http |
addParam(Map<String,?> params)
最终是拼接成queryString的形式追加到url(即作为get的http请求参数)
get方式会有编码等问题,推荐使用data方式传参数:
data(Map) |
<T> Http |
addParam(String name,
T value) |
Http |
addPart(String formName,
String fileName,
Object mimePart) |
Http |
addPart(String formName,
String fileName,
String contentType,
Object mimePart)
文件上传
|
Http |
connTimeoutSeconds(int seconds)
set connect timeout
|
Http |
contentType(ContentType contentType) |
Http |
contentType(ContentType contentType,
String contentCharset)
请求实体报头,发送信息至服务器时内容编码类型:
multipart/form-data,application/x-www-form-urlencoded,
application/json
默认:application/x-www-form-urlencoded
调用方式:contentType("application/json", "UTF-8")
|
Http |
data(Map<String,?> data)
发送到服务器的查询字符串或json串:name1=value1&name2=value2
|
Http |
data(Map<String,?> data,
String charset)
发送到服务器的查询字符串或json串:name1=value1&name2=value2
application/x-www-form-urlencoded
|
Http |
data(String data)
HttpURLConnection.getOutputStream().write(data)
|
static Http |
delete(String url) |
byte[] |
download() |
void |
download(OutputStream output)
http下载
|
void |
download(String filepath) |
Http |
encode(Boolean encode)
编码url
|
static Http |
get(String url) |
Map<String,String> |
getReqHeaders() |
String |
getRespHeader(String name) |
Map<String,List<String>> |
getRespHeaders() |
String[] |
getRespHeaders(String name) |
HttpStatus |
getStatus() |
static Http |
head(String url) |
static Http |
of(String url,
Http.HttpMethod method) |
static Http |
of(String url,
String method) |
static Http |
options(String url) |
static Http |
post(String url) |
static Http |
put(String url) |
Http |
readTimeoutSeconds(int seconds)
set read timeout
|
String |
request()
发送请求获取响应数据
|
<T> T |
request(Class<T> type) |
<T> T |
request(com.fasterxml.jackson.databind.JavaType type) |
Http |
setSSLSocketFactory(SSLContext sslContext) |
Http |
setSSLSocketFactory(SSLSocketFactory factory)
trust spec certificate
|
static Http |
trace(String url) |
public static Http of(String url, Http.HttpMethod method)
public Http addParam(Map<String,?> params)
data(Map)params - public Http data(Map<String,?> data)
data - the http body payload datapublic Http data(Map<String,?> data, String charset)
data - the http body payload datacharset - the charsetpublic Http data(String data)
data - the http body datapublic Http addPart(String formName, String fileName, String contentType, Object mimePart)
formName - 表单名称fileName - 附件名称contentType - 附件类型,value of the Content-Type part headermimePart - 上传数据public Http contentType(ContentType contentType, String contentCharset)
请求实体报头,发送信息至服务器时内容编码类型:
multipart/form-data,application/x-www-form-urlencoded,
application/json
默认:application/x-www-form-urlencoded
调用方式:contentType("application/json", "UTF-8")
contentType - contentCharset - public Http contentType(ContentType contentType)
public Http accept(ContentType contentType)
contentType - application/jsonpublic Http connTimeoutSeconds(int seconds)
seconds - (s)public Http readTimeoutSeconds(int seconds)
seconds - (s)public Http setSSLSocketFactory(SSLSocketFactory factory)
factory - public Http setSSLSocketFactory(SSLContext sslContext)
public <T> T request(com.fasterxml.jackson.databind.JavaType type)
public <T> T request(Class<T> type)
public String request()
public void download(String filepath)
public byte[] download()
public void download(OutputStream output)
output - output to stream of response datapublic HttpStatus getStatus()
Copyright © 2023. All rights reserved.