http客户端
Jboot内置了一个轻量级的http客户端,可以通过这个客户端方便的对其他第三方http服务器进行数据请求和下载等功能。
Get请求
@Test
public void testHttpGet(){
String html = Jboot.httpGet("https://www.baidu.com");
System.out.println(html);
}
或者
@Test
public void testHttpPost(){
Map<String, Object> params = new HashMap<>();
params.put("key1","value1");
params.put("key2","value2");
String html = Jboot.httpGet("http://www.oschina.net/",params);
System.out.println(html);
}
Post请求
@Test
public void testHttpPost(){
String html = Jboot.httpPost("http://www.xxx.com");
System.out.println(html);
}
或者
@Test
public void testHttpPost(){
Map<String, Object> params = new HashMap<>();
params.put("key1","value1");
params.put("key2","value2");
String html = Jboot.httpPost("http://www.oschina.net/",params);
System.out.println(html);
}
文件上传
@Test
public void testHttpUploadFile(){
Map<String, Object> params = new HashMap<>();
params.put("file1",file1);
params.put("file2",file2);
String html = Jboot.httpPost("http://www.oschina.net/",params);
System.out.println(html);
}
备注:文件上传其实和post提交是一样的,只是params中的参数是文件。
文件下载
@Test
public void testHttpDownload() {
String url = "http://www.xxx.com/abc.zip";
File downloadToFile = new File("/xxx/abc.zip");
JbootHttpRequest request = JbootHttpRequest.create(url, null, JbootHttpRequest.METHOD_GET);
request.setDownloadFile(downloadToFile);
JbootHttpResponse response = Jboot.me().getHttp().handle(request);
if (response.isError()){
downloadToFile.delete();
}
System.out.println(downloadToFile.length());
}