public class HttpRequest extends Object implements Closeable
| Modifier and Type | Class and Description |
|---|---|
class |
HttpRequest.BufferedResponse
Used to buffer the response in memory.
|
static class |
HttpRequest.Form
Used with
bodyForm(Form). |
| Modifier and Type | Method and Description |
|---|---|
HttpRequest |
body(String data)
Submit data.
|
HttpRequest |
bodyForm(HttpRequest.Form form)
Submit form data.
|
void |
close() |
HttpRequest |
execute()
Execute the request.
|
HttpRequest |
expectResponseCode(int... codes)
Require that the response code is one of the given response codes.
|
static HttpRequest |
get(URL url)
Perform a GET request.
|
InputStream |
getInputStream()
Get the input stream.
|
int |
getResponseCode()
Get the response code.
|
HttpRequest |
header(String key,
String value)
Add a header.
|
static HttpRequest |
post(URL url)
Perform a POST request.
|
static HttpRequest |
request(String method,
URL url)
Perform a request.
|
HttpRequest.BufferedResponse |
returnContent()
Buffer the returned response.
|
HttpRequest |
saveContent(File file)
Save the result to a file.
|
HttpRequest |
saveContent(OutputStream out)
Save the result to an output stream.
|
static URL |
url(String url)
Create a new
URL and throw a RuntimeException if the URL
is not valid. |
public HttpRequest body(String data)
public HttpRequest bodyForm(HttpRequest.Form form)
form - the formpublic HttpRequest header(String key, String value)
key - the header keyvalue - the header valuepublic HttpRequest execute() throws IOException
close() should be called.IOException - on I/O errorpublic HttpRequest expectResponseCode(int... codes) throws IOException
codes - a list of codesIOException - if there is an I/O error or the response code is not expectedpublic int getResponseCode()
throws IOException
IOException - on I/O errorpublic InputStream getInputStream()
public HttpRequest.BufferedResponse returnContent() throws IOException, InterruptedException
IOException - on I/O errorInterruptedException - on interruptionpublic HttpRequest saveContent(File file) throws IOException, InterruptedException
file - the fileIOException - on I/O errorInterruptedException - on interruptionpublic HttpRequest saveContent(OutputStream out) throws IOException, InterruptedException
out - the output streamIOException - on I/O errorInterruptedException - on interruptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionpublic static HttpRequest get(URL url)
url - the URLpublic static HttpRequest post(URL url)
url - the URLpublic static HttpRequest request(String method, URL url)
method - the methodurl - the URLpublic static URL url(String url)
URL and throw a RuntimeException if the URL
is not valid.url - the urlRuntimeException - if the URL is invalidCopyright © 2015. All Rights Reserved.