使用Java客户端来访问Api(resty-client)
1. 使用Resty-client客户端来验证Api的权限,当使用了登录参数,api访问403没有权限时,会先进行登录,以通过权限验证
Client client = new Client("http://127.0.0.1:8081/api/v1.0", "/sessions", "awesa", "123", false);
Client client = new Client("http://127.0.0.1:8081/api/v1.0");
2. 使用client来执行表单请求,返回请求结果ClientResult
ClientRequest request = new ClientRequest("/users/1");
uploadRequest.addParameter("parameter", "我是参数");
ClientResult result=client.build(request).post();
3. 使用client来发送application/json数据请求
ClientRequest request = new ClientRequest("/users/1");
request.setJsonParameter(jsonString);
ResponseData result=client.build(request).post();
4. 使用client来上传文件
ClientRequest uploadRequest = new ClientRequest("/tests/resty");
uploadRequest.addUploadFile("resty", ClientTest.class.getResource("/resty.jar").getFile());
uploadRequest.addParameter("des", "test file paras 测试笔");
ClientResult uploadResult = client.build(uploadRequest).post();
5. 使用client来下载文件,支持断点续传
ClientRequest downloadRequest = new ClientRequest("/tests/file");
/第二个参数overwrite可以没有,表示遇到同名文件是否覆盖
downloadRequest.setDownloadFile(ClientTest.class.getResource("/").getFile(),false);
ClientResult downloadResult = client.build(downloadRequest).get();