摘自:https://blog.csdn.net/mbh12333/article/details/78330158
一个简单的Get请求
HttpRequest httpRequest = HttpRequest.get("http://jodd.org");
HttpResponse response = httpRequest.send();
//更简洁的一种写法 HttpResponse response = HttpRequest.get("http://jodd.org").send();
System.out.println(response);
输出结果如下:
//下面是响应头信息
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=0, public
Connection: close
Content-Length: 15441
Content-Type: text/html; charset=utf-8
Date: Mon, 04 Jul 2016 14:26:08 GMT
Expires: Mon, 04 Jul 2016 14:26:08 GMT
Last-Modified: Thu, 24 Mar 2016 23:05:05 GMT
Server: nginx/1.10.1
Vary: Accept-Encoding
<!DOCTYPE html>
<html>
......
</html>
//省略部分是访问页面的HTML页面
较为复杂一点的get请求:
HttpRequest request = new HttpRequest();
request
.method("GET")//get请求
.protocol("http")//协议使用http
.host("jodd.org")//主机地址
.port(80)//端口,没有写默认是80
.path("/api/jsonws/user/get-user-by-id");//访问路径
HttpResponse对象中有下面三个方法:
body() - 返回ISO-8859-1编码的response返回体(上面的访问对应的是jodd的html源码)
bodyText() - 返回与响应头中相应编码的response返回体
bodyBytes() - 返回返回体的字节码
参数
//get访问中直接在URL中传递参数
HttpResponse response = HttpRequest
.get("http://srv:8080/api/jsonws/user/get-user-by-id?userId=10194")
.send();
//调用方法的方式传递参数
HttpResponse response = HttpRequest
.get("http://srv:8080/api/jsonws/user/get-user-by-id")
.query("userId", "10194")
.send();
//或者创建request对象后用以下map的方式传递参数
Map<String, Object[]> httpParams = request.query();
httpParams.put("userId", new String[] {"10194"});
基本的用户认证请求
request.basicAuthentication("test", "test");//是不是很方便啊
POST请求
HttpResponse response = HttpRequest
.post("http://srv:8080/api/jsonws/user/get-user-by-id")
.form("userId", "10194")//这里与get的参数传递方式不同
.send();
上传文件
HttpRequest httpRequest = HttpRequest
.post("http://srv:8080/api/jsonws/dlapp/add-file-entry")
.form(
"repositoryId", "10178",
"folderId", "11219",
"sourceFileName", "a.zip",
"mimeType", "application/zip",
"title", "test",
"description", "Upload test",
"changeLog", "testing...",
"file",new File("d:\\a.jpg.zip")
);
HttpResponse httpResponse = httpRequest.send();
还可以设置回调方法监听上传的进度等:
HttpResponse response = HttpRequest
.post("http://localhost:8081/hello")
.form("file", file)
.monitor(new HttpProgressListener() {//监听上传的进度
@Override
public void transferred(long len) {
System.out.println(len/size);
}
})
.send();
请求头等信息的设置 HttpRequest中提供了header的多种不同方法签名的传递header的方法,还提供headers使用map传递header的方法使用起来十分方便。 此为还提供contentType、cookie的等方法操作相关内容【具体可以看HttpRequest的API】
GZIP
有些网站使用了GZIP压缩技术,这就要求获得请求的时候使用GZIP解压才能获得数据,jodd提供了相关的支持而且使用十分简单:
HttpResponse response = HttpRequest
.get("http://www.liferay.com")
.acceptEncoding("gzip")
.send();
System.out.println(response.unzip());
body
HttpResponse response = HttpRequest
.get("http://srv:8080/api/jsonws/invoke")
.body("{‘$user[userId, screenName] = /user/get-user-by-id‘ : {‘userId‘:‘10194‘}}")
.basicAuthentication("test", "test")
.send();
编码设置
HttpResponse response = HttpRequest
.get("http://server/index.html")
.queryEncoding("CP1251")
.query("param", "value")
.send();
Soket连接
HttpRequest request = HttpRequest.get()...;
request.open();
SocketHttpConnection httpConnection =
(SocketHttpConnection) request.httpConnection();
Socket socket = httpConnection.getSocket();
socket.setSoTimeout(1000);
...
HttpResponse response = request.send();
长连接
HttpRequest request = HttpRequest.get("http://jodd.org");
HttpResponse response = request.connectionKeepAlive(true).send();
// next request
request = HttpRequest.get("http://jodd.org/jodd.css");
response = request.keepAlive(response, true).send();
...
// last request
request = HttpRequest.get("http://jodd.org/jodd.png");
response = request.keepAlive(response, false).send();
// optionally
//response.close();