public class HttpRequest
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Class and Description |
---|---|
class |
HttpRequest.BufferedResponse
Used to buffer the response in memory.
|
static class |
HttpRequest.Form
Used with
bodyUrlEncodedForm(Form) . |
Modifier and Type | Method and Description |
---|---|
HttpRequest |
body(java.lang.String data)
Submit data.
|
HttpRequest |
bodyMultipartForm(HttpRequest.Form form)
Submit form data.
|
HttpRequest |
bodyUrlEncodedForm(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(java.net.URL url)
Perform a GET request.
|
java.io.InputStream |
getInputStream()
Get the input stream.
|
int |
getResponseCode()
Get the response code.
|
HttpRequest |
header(java.lang.String key,
java.lang.String value)
Add a header.
|
static HttpRequest |
post(java.net.URL url)
Perform a POST request.
|
static HttpRequest |
request(java.lang.String method,
java.net.URL url)
Perform a request.
|
HttpRequest.BufferedResponse |
returnContent()
Buffer the returned response.
|
HttpRequest |
saveContent(java.io.File file)
Save the result to a file.
|
HttpRequest |
saveContent(java.io.OutputStream out)
Save the result to an output stream.
|
static java.net.URL |
url(java.lang.String url)
Create a new
URL and throw a RuntimeException if the URL
is not valid. |
public HttpRequest body(java.lang.String data)
public HttpRequest bodyUrlEncodedForm(HttpRequest.Form form)
form
- the formpublic HttpRequest bodyMultipartForm(HttpRequest.Form form)
form
- the formpublic HttpRequest header(java.lang.String key, java.lang.String value)
key
- the header keyvalue
- the header valuepublic HttpRequest execute() throws java.io.IOException
close()
should be called.java.io.IOException
- on I/O errorpublic HttpRequest expectResponseCode(int... codes) throws java.io.IOException
codes
- a list of codesjava.io.IOException
- if there is an I/O error or the response code is not expectedpublic int getResponseCode() throws java.io.IOException
java.io.IOException
- on I/O errorpublic java.io.InputStream getInputStream()
public HttpRequest.BufferedResponse returnContent() throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
- on I/O errorjava.lang.InterruptedException
- on interruptionpublic HttpRequest saveContent(java.io.File file) throws java.io.IOException, java.lang.InterruptedException
file
- the filejava.io.IOException
- on I/O errorjava.lang.InterruptedException
- on interruptionpublic HttpRequest saveContent(java.io.OutputStream out) throws java.io.IOException, java.lang.InterruptedException
out
- the output streamjava.io.IOException
- on I/O errorjava.lang.InterruptedException
- on interruptionpublic void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public static HttpRequest get(java.net.URL url)
url
- the URLpublic static HttpRequest post(java.net.URL url)
url
- the URLpublic static HttpRequest request(java.lang.String method, java.net.URL url)
method
- the methodurl
- the URLpublic static java.net.URL url(java.lang.String url)
URL
and throw a RuntimeException
if the URL
is not valid.url
- the urljava.lang.RuntimeException
- if the URL is invalid