你在评论中写道,你"希望得到比HttpClient更高级别的东西".听起来Restlet会很完美.它提供了一个用于实现和使用 RESTful Web应用程序的高级API,以及用于较低级别实现的即插即用适配器.
例如,要使用Restlet 1.1将webform发布到资源:
Client client = new Client(Protocol.HTTP);
Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");
Response response = client.post("http://host/path/to/resource", form.getWebRepresentation())
if (response.getStatus().isError()) {
// deal with the error
return;
}
if (response.isEntityAvailable()) {
System.out.println(response.getEntity().getText());
}
Run Code Online (Sandbox Code Playgroud)
如果需要在请求上设置更多选项,可以使用Request对象:
Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");
Request request = new Request(Method.POST, "http://host/path/to/resource");
request.setEntity(form.getWebRepresentation());
request.setReferrerRef("http://host/path/to/referrer");
Response response = client.handle(request);
Run Code Online (Sandbox Code Playgroud)
HTH!