public abstract class HttpRequest
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
HttpRequest.HttpRequestFactory |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
HEADER_ACCEPT |
static java.lang.String |
HEADER_CONTENT_LENGTH |
static java.lang.String |
HEADER_CONTENT_TYPE |
static java.lang.String |
HEADER_USER_AGENT |
static java.lang.String |
METHOD_DELETE |
static java.lang.String |
METHOD_GET |
static java.lang.String |
METHOD_POST |
static java.lang.String |
METHOD_PUT |
Constructor and Description |
---|
HttpRequest() |
Modifier and Type | Method and Description |
---|---|
static HttpRequest |
create(java.lang.String url)
Create an HttpRequest, using the global factory.
|
abstract OutputStream |
createRequestBodyStream()
Returns an output stream that writes to this connection.
|
abstract java.lang.String |
getHeader(java.lang.String name)
Returns the value of the named header or null if it's not set.
|
abstract HttpResponse |
send()
Opens a communications link to the resource referenced by this URL, if such a connection has not already been
established.
|
void |
setContentTypeAndAcceptHeaders(java.lang.String contentType,
java.lang.String accept)
This is a convenience method to set both the Content-Type and Accept headers, at the same time.
|
static void |
setFactory(HttpRequest.HttpRequestFactory httpRequestFactory)
Set the global (default) factory used to create http requests.
|
abstract void |
setHeader(java.lang.String name,
java.lang.String value)
Sets the specified HTTP request header to the specified value.
|
abstract void |
setMethod(java.lang.String method)
Set the method for the request, one of:
GET POST HEAD OPTIONS PUT DELETE TRACE
are legal, subject to protocol restrictions.
|
abstract void |
setTimeout(int timeoutInMillis)
Sets a specified timeout value, in milliseconds, to be used when doing connecting/reading/writing to the server.
|
public static final java.lang.String METHOD_GET
public static final java.lang.String METHOD_POST
public static final java.lang.String METHOD_PUT
public static final java.lang.String METHOD_DELETE
public static final java.lang.String HEADER_CONTENT_LENGTH
public static final java.lang.String HEADER_CONTENT_TYPE
public static final java.lang.String HEADER_USER_AGENT
public static final java.lang.String HEADER_ACCEPT
public static HttpRequest create(java.lang.String url)
url
- URLpublic static void setFactory(@Nullable HttpRequest.HttpRequestFactory httpRequestFactory)
httpRequestFactory
- factorypublic abstract void setMethod(java.lang.String method)
method
- the HTTP methodpublic void setContentTypeAndAcceptHeaders(java.lang.String contentType, java.lang.String accept)
contentType
- Content-Type header valueaccept
- Accept header valuepublic abstract void setTimeout(int timeoutInMillis)
timeoutInMillis
- an int
that specifies the connect timeout value in millisecondspublic abstract void setHeader(java.lang.String name, java.lang.String value)
name
- the header name (e.g., "Accept
")value
- the value associated with itpublic abstract java.lang.String getHeader(java.lang.String name)
name
- the header name (e.g., "Accept").public abstract OutputStream createRequestBodyStream()
IOException
- if an I/O error occurs while opening the connection.public abstract HttpResponse send()
connect
method is called when the connection has already been opened.
HttpRequest objects go through two phases: first they are created, then they are connected. After being created,
and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it
is an error to try to set them. Operations that depend on being connected, like getContentLength, will
implicitly perform the connection, if necessary.SocketTimeoutException
- if the timeout expires before the connection can be establishedIOException
- if an I/O error occurs while opening the connection.setTimeout(int)