写这个库是为了在使用HttpURLConnection
来发送HTTP请求的时候更加方便快捷
像Apache HttpComponents这样的组件也是非常好用的,但是有些时候为了更加简单,或者可能因为你部署的环境的问题(比如Android),你只想使用例如HttpURLConnection
一些过时但是又好用的库。
这个库寻找一种更加方便和一种更加通用的模式来模拟HTTP请求,并支持多种特性的请求,例如多个请求的任务.
int response = HttpRequest.get("http://google.com").code();
String response = HttpRequest.get("http://google.com").body();
System.out.println("Response was: " + response);
HttpRequest.get("http://google.com").receive(System.out);
HttpRequest request = HttpRequest.get("http://google.com", true, 'q', "baseball gloves", "size", 100);
System.out.println(request.toString()); // GET http://google.com?q=baseball%20gloves&size=100
int[] ids = new int[] { 22, 23 };
HttpRequest request = HttpRequest.get("http://google.com", true, "id", ids);
System.out.println(request.toString()); // GET http://google.com?id[]=22&id[]=23
String contentType = HttpRequest.get("http://google.com")
.accept("application/json") //Sets request header
.contentType(); //Gets response header
System.out.println("Response content type was " + contentType);
int response = HttpRequest.post("http://google.com").send("name=kevin").code();
int response = HttpRequest.get("http://google.com").basic("username", "p4ssw0rd").code();
HttpRequest request = HttpRequest.post("http://google.com");
request.part("status[body]", "Making a multipart request");
request.part("status[image]", new File("/home/kevin/Pictures/ide.png"));
if (request.ok())
System.out.println("Status was updated");
Map<String, String> data = new HashMap<String, String>();
data.put("user", "A User");
data.put("state", "CA");
if (HttpRequest.post("http://google.com").form(data).created())
System.out.println("User was created");
File output = new File("/output/request.out");
HttpRequest.get("http://google.com").receive(output);
File input = new File("/input/data.txt");
int response = HttpRequest.post("http://google.com").send(input).code();
String base64 = base64(t[i].getAbsolutePath());
String body = HttpRequest.post(url)
.part("base64Strs", new ByteArrayInputStream(base64.getBytes())).body();
File latest = new File("/data/cache.json");
HttpRequest request = HttpRequest.get("http://google.com");
//Copy response to file
request.receive(latest);
//Store eTag of response
String eTag = request.eTag();
//Later on check if changes exist
boolean unchanged = HttpRequest.get("http://google.com")
.ifNoneMatch(eTag)
.notModified();
HttpRequest request = HttpRequest.get("http://google.com");
//Tell server to gzip response and automatically uncompress
request.acceptGzipEncoding().uncompress(true);
String uncompressed = request.body();
System.out.println("Uncompressed response is: " + uncompressed);
HttpRequest request = HttpRequest.get("https://google.com");
//Accept all certificates
request.trustAllCerts();
//Accept all hostnames
request.trustAllHosts();
HttpRequest request = HttpRequest.get("https://google.com");
//Configure proxy
request.useProxy("localhost", 8080);
//Optional proxy basic authentication
request.proxyBasic("username", "p4ssw0rd");
int code = HttpRequest.get("http://google.com").followRedirects(true).code();
HttpRequest.setConnectionFactory(new ConnectionFactory() {
public HttpURLConnection create(URL url) throws IOException {
if (!"https".equals(url.getProtocol()))
throw new IOException("Only secure requests are allowed");
return (HttpURLConnection) url.openConnection();
}
public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
if (!"https".equals(url.getProtocol()))
throw new IOException("Only secure requests are allowed");
return (HttpURLConnection) url.openConnection(proxy);
}
});