Requests is a http request lib with fluent api for java, inspired by the python request module.
Requests is now in maven central repo.
<dependency> <groupId>net.dongliu</groupId> <artifactId>requests</artifactId> <version>4.9.0</version> </dependency>One simple http request example that do http get request and read response as string:
Stringurl = ...; Stringresp = Requests.get(url).send().readToText(); // orResponse<String> resp = Requests.get(url).send().toTextResponse();Post and other method:
resp = Requests.post(url).send().readToText(); resp = Requests.head(url).send().readToText(); ...The response object have several common http response fields can be used:
RawResponseresp = Requests.get(url).send(); intstatusCode = resp.getStatusCode(); List<Parameter<String>> headers = resp.getHeaders(); Collection<Cookie> cookies = resp.getCookies(); Stringbody = resp.readToText();Make sure call readToText or other methods to consume resp, or call close method to close resp.
The readToText() method here trans http response body as String, more other methods provided:
// get response as string, use encoding get from response headerStringresp = Requests.get(url).send().readToText(); // get response as bytesbyte[] resp1 = Requests.get(url).send().readToBytes(); // save response as filebooleanresult = Requests.get(url).send().writeToFile("/path/to/save/file");Requests default use UTF-8 to encode parameters, post forms or request string body, you can set other charset by:
Stringresp = Requests.get(url).requestCharset(StandardCharsets.ISO_8859_1).send().readToText();When read response to text-based result, use charset get from http response header, or UTF-8 if not found. You can force use specified charset by:
Stringresp = Requests.get(url).send().withCharset(StandardCharsets.ISO_8859_1).readToText();Pass parameters in urls using params method:
// set params by mapMap<String, Object> params = newHashMap<>(); params.put("k1", "v1"); params.put("k2", "v2"); Stringresp = Requests.get(url).params(params).send().readToText(); // set multi paramsStringresp = Requests.get(url).params(Parameter.of("k1", "v1"), Parameter.of("k2", "v2")) .send().readToText();If you want to send post www-form-encoded parameters, use forms() methods:
// set params by mapMap<String, Object> params = newHashMap<>(); params.put("k1", "v1"); params.put("k2", "v2"); Stringresp = Requests.post(url).forms(params).send().readToText(); // set multi paramsStringresp = Requests.post(url).forms(Parameter.of("k1", "v1"), Parameter.of("k2", "v2")) .send().readToText();The forms parameter should only works with post method.
Http request headers can be set by headers method:
// set headers by mapMap<String, Object> headers = newHashMap<>(); headers.put("k1", "v1"); headers.put("k2", "v2"); Stringresp = Requests.get(url).headers(headers).send().readToText(); // set multi headersStringresp = Requests.get(url).headers(Parameter.of("k1", "v1"), Parameter.of("k2", "v2")) .send().readToText();Cookies can be add by:
Map<String, Object> cookies = newHashMap<>(); cookies.put("k1", "v1"); cookies.put("k2", "v2"); // set cookies by mapStringresp = Requests.get(url).cookies(cookies).send().readToText(); // set cookiesStringresp = Requests.get(url).cookies(Parameter.of("k1", "v1"), Parameter.of("k2", "v2")) .send().readToText();Http Post, Put, Patch method can send request body. Take Post for example:
// set post form dataStringresp = Requests.post(url).forms(Parameter.of("k1", "v1"), Parameter.of("k2", "v2")) .send().readToText(); // set post form data by mapMap<String, Object> formData = newHashMap<>(); formData.put("k1", "v1"); formData.put("k2", "v2"); Stringresp = Requests.post(url).forms(formData).send().readToText(); // send byte array data as bodybyte[] data = ...; resp = Requests.post(url).body(data).send().readToText(); // send string data as bodyStringstr = ...; resp = Requests.post(url).body(str).send().readToText(); // send data from inputStreamInputStreamin = ... resp = Requests.post(url).body(in).send().readToText();One more complicate situation is multiPart post request, this can be done via multiPart method, one simplified multi part request example which send files and param data:
// send form-encoded dataInputStreamin = ...; byte[] bytes = ...; Stringresp = Requests.post(url) .multiPartBody(Part.file("file1", newFile(...)), Part.file("file2", newFile("...")), Part.param("input", "on")) .send().readToText();Set http basic auth param by auth method:
Stringresp = Requests.get(url).basicAuth("user", "passwd").send().readToText();Requests will handle 30x http redirect automatically, you can disable it by:
Requests.get(url).followRedirect(false).send();There are two timeout parameters you can set, connect timeout, and socket timeout. The timeout value default to 10_000 milliseconds.
// both connec timeout, and socket timeoutRequests.get(url).timeout(30_000).send(); // set connect timeout and socket timeout separatelyRequests.get(url).socketTimeout(20_000).connectTimeout(30_000).send();Requests send Accept-Encoding: gzip, deflate, and handle gzipped response in default. You can disable this by:
Requests.get(url).compress(false).send();Some https sites do not have trusted http certificate, Exception will be thrown when request. You can disable https certificate verify by:
Requests.get(url).verify(false).send();Set proxy by proxy method:
Requests.get(url).proxy(Proxies.httpProxy("127.0.0.1", 8081)).send(); // http proxyRequests.get(url).proxy(Proxies.socksProxy("127.0.0.1", 1080)).send(); // socks proxy proxySession maintains cookies, basic auth and maybe other http context for you, useful when need login or other situations. Session have the same usage as Requests.
Sessionsession = Requests.session(); Stringresp1 = session.get(url1).send().readToText(); Stringresp2 = session.get(url2).send().readToText();