public class HTTPRequest
extends java.lang.Object
Scripts can set default values for the URL, headers, and data. There are several overloaded methods corresponding to each HTTP method (GET, POST, ...) that allow specific values to override the defaults.
WARNING: The default values set with the various
set
methods, and apply to all users of the
HTTPRequest
. If a worker thread needs to set a thread specific
value, it should either use its own HTTPRequest
, or not use the
defaults and pass the value as an argument to the HTTP method.
Content-Length
header has been supplied - please refer to the
HttpOutputStream
class Javadoc for full details. If you are
streaming output to avoid loading complete request messages into memory,
you may also want to use setReadResponseBody(boolean)
to disable
the reading of response bodies, and managed them yourself.Constructor and Description |
---|
HTTPRequest()
Creates a new
HTTPRequest instance. |
Modifier and Type | Method and Description |
---|---|
HTTPResponse |
DELETE()
Makes an HTTP
DELETE request. |
HTTPResponse |
DELETE(java.lang.String uri)
Makes an HTTP
DELETE request. |
HTTPResponse |
DELETE(java.lang.String uri,
NVPair[] headers)
Makes an HTTP
DELETE request. |
HTTPResponse |
GET()
Makes an HTTP
GET request. |
HTTPResponse |
GET(NVPair[] queryData)
Makes an HTTP
GET request. |
HTTPResponse |
GET(java.lang.String uri)
Makes an HTTP
GET request. |
HTTPResponse |
GET(java.lang.String uri,
NVPair[] queryData)
Makes an HTTP
GET request. |
HTTPResponse |
GET(java.lang.String uri,
NVPair[] queryData,
NVPair[] headers)
Makes an HTTP
GET request. |
byte[] |
getData()
Gets the default data.
|
NVPair[] |
getFormData()
Gets the default form data.
|
NVPair[] |
getHeaders()
Gets the default headers.
|
static Test.InstrumentationFilter |
getHttpMethodFilter()
Return an instrumentation filter that selects only the HTTP request
methods, i.e.
|
protected PluginProcessContext |
getPluginProcessContext()
Provide subclasses access to the process context.
|
boolean |
getReadResponseBody()
Return whether or not the whole response body will be read.
|
java.lang.String |
getUrl()
Gets the default URL.
|
HTTPResponse |
HEAD()
Makes an HTTP
HEAD request. |
HTTPResponse |
HEAD(NVPair[] queryData)
Makes an HTTP
HEAD request. |
HTTPResponse |
HEAD(java.lang.String uri)
Makes an HTTP
HEAD request. |
HTTPResponse |
HEAD(java.lang.String uri,
NVPair[] queryData)
Makes an HTTP
HEAD request. |
HTTPResponse |
HEAD(java.lang.String uri,
NVPair[] queryData,
NVPair[] headers)
Makes an HTTP
HEAD request. |
HTTPResponse |
OPTIONS()
Makes an HTTP
OPTIONS request. |
HTTPResponse |
OPTIONS(java.lang.String uri)
Makes an HTTP
OPTIONS request. |
HTTPResponse |
OPTIONS(java.lang.String uri,
byte[] data)
Makes an HTTP
OPTIONS request. |
HTTPResponse |
OPTIONS(java.lang.String uri,
byte[] data,
NVPair[] headers)
Makes an HTTP
OPTIONS request. |
HTTPResponse |
OPTIONS(java.lang.String uri,
java.io.InputStream inputStream)
Makes an HTTP
OPTIONS request. |
HTTPResponse |
OPTIONS(java.lang.String uri,
java.io.InputStream inputStream,
NVPair[] headers)
Makes an HTTP
OPTIONS request. |
HTTPResponse |
POST()
Makes an HTTP
POST request. |
HTTPResponse |
POST(NVPair[] formData)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
byte[] data)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
byte[] data,
NVPair[] headers)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
java.io.InputStream inputStream)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
java.io.InputStream inputStream,
NVPair[] headers)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
NVPair[] formData)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
NVPair[] formData,
NVPair[] headers)
Makes an HTTP
POST request. |
HTTPResponse |
POST(java.lang.String uri,
NVPair[] formData,
NVPair[] headers,
boolean isMultipart)
Makes an HTTP
POST request. |
protected void |
processResponse(HTTPResponse response)
Subclasses of HTTPRequest that wish to post-process responses
should override this method.
|
HTTPResponse |
PUT()
Makes an HTTP
PUT request. |
HTTPResponse |
PUT(java.lang.String uri)
Makes an HTTP
PUT request. |
HTTPResponse |
PUT(java.lang.String uri,
byte[] data)
Makes an HTTP
PUT request. |
HTTPResponse |
PUT(java.lang.String uri,
byte[] data,
NVPair[] headers)
Makes an HTTP
PUT request. |
HTTPResponse |
PUT(java.lang.String uri,
java.io.InputStream inputStream)
Makes an HTTP
PUT request. |
HTTPResponse |
PUT(java.lang.String uri,
java.io.InputStream inputStream,
NVPair[] headers)
Makes an HTTP
PUT request. |
void |
setData(byte[] data)
Sets the default data.
|
byte[] |
setDataFromFile(java.lang.String filename)
Sets the default data from a file.
|
void |
setFormData(NVPair[] formData)
Sets the default form data.
|
void |
setHeaders(NVPair[] headers)
Sets the default headers.
|
void |
setReadResponseBody(boolean b)
Set whether or not the whole response body will be read.
|
void |
setUrl(java.lang.String url)
Sets the default URL.
|
java.lang.String |
toString()
Returns a string representation of the object and URL headers.
|
HTTPResponse |
TRACE()
Makes an HTTP
TRACE request. |
HTTPResponse |
TRACE(java.lang.String uri)
Makes an HTTP
TRACE request. |
HTTPResponse |
TRACE(java.lang.String uri,
NVPair[] headers)
Makes an HTTP
TRACE request. |
public final java.lang.String getUrl()
null
if the default URL has not been set.public final void setUrl(java.lang.String url) throws ParseException, URLException
See the warning above
regarding thread safety.
Multiple worker threads that need to set a specific URL should either not
share the same HTTPRequest
, or pass the URL as an argument
to the call to the HTTP method.
url
- The URL to be used for this request.ParseException
- If the URL cannot be parsed.URLException
- If the URL is not absolute.public final NVPair[] getHeaders()
public final void setHeaders(NVPair[] headers)
See the warning above
regarding thread safety.
Multiple worker threads that need to set specific headers should either not
share the same HTTPRequest
, or pass the headers as an argument
to the call to the HTTP method.
headers
- The default headers to be used for this request.public java.lang.String toString()
toString
in class java.lang.Object
public final byte[] getData()
public final void setData(byte[] data)
See the warning above
regarding thread safety.
Multiple worker threads that need to set specific data should either not
share the same HTTPRequest
, or pass the data as an argument
to the call to POST
, or PUT
.
data
- The default data to be used for this request.public final byte[] setDataFromFile(java.lang.String filename) throws java.io.IOException
See the warning above
regarding thread safety.
Multiple worker threads that need to set specific data should either not
share the same HTTPRequest
, or pass the data as an argument
to the call to POST
, or PUT
. If the later is
done, this method can still be used to read data from a file.
filename
- Path name of data file.java.io.IOException
- If the file could not be read.public final NVPair[] getFormData()
public final void setFormData(NVPair[] formData)
formData
- The default form or query data to be used for
this request.public boolean getReadResponseBody()
true
=> The response body will be read.setReadResponseBody(boolean)
public void setReadResponseBody(boolean b)
If true
, the response body will be read during one
of the HTTP method operations (GET
, PUT
, ...).
Otherwise, the response body will not be read. Most users will want
to leave this set to its default value of true
.
If set to false
, the response body stream will be
available for reading from the HTTPResponse.getInputStream()
, and
the following effects will be observed for the test statistics:
0
.
If desired, the caller could manually update the statistics by extending
HTTPRequest
and implementing processResponse(HTTPClient.HTTPResponse)
as follows:
final PluginProcessContext pluginProcessContext = getPluginProcessContext(); final HTTPPluginThreadState threadState = (HTTPPluginThreadState)pluginProcessContext.getPluginThreadListener(); final PluginThreadContext threadContext = threadState.getThreadContext(); threadContext.resumeClock(); final int bodyLength = ... // Read body from HTTPResponse. threadContext.pauseClock(); final StatisticsForTest testStatistics = statistics.getForCurrentTest(); testStatistics.addLong( StatisticsIndexMap.HTTP_PLUGIN_RESPONSE_LENGTH_KEY, bodyLength);
b
- true
=> The response body will be read.public final HTTPResponse DELETE() throws java.lang.Exception
DELETE
request.java.lang.Exception
- If an error occurs.public final HTTPResponse DELETE(java.lang.String uri) throws java.lang.Exception
DELETE
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse DELETE(java.lang.String uri, NVPair[] headers) throws java.lang.Exception
DELETE
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse GET() throws java.lang.Exception
GET
request.java.lang.Exception
- If an error occurs.public final HTTPResponse GET(java.lang.String uri) throws java.lang.Exception
GET
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse GET(NVPair[] queryData) throws java.lang.Exception
GET
request.queryData
- Request headers. Replaces all the values set
by setFormData(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse GET(java.lang.String uri, NVPair[] queryData) throws java.lang.Exception
GET
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.queryData
- Request headers. Replaces all the values set
by setFormData(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse GET(java.lang.String uri, NVPair[] queryData, NVPair[] headers) throws java.lang.Exception
GET
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.queryData
- Request headers. Replaces all the values set by
setFormData(HTTPClient.NVPair[])
.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse HEAD() throws java.lang.Exception
HEAD
request.java.lang.Exception
- If an error occurs.public final HTTPResponse HEAD(java.lang.String uri) throws java.lang.Exception
HEAD
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse HEAD(NVPair[] queryData) throws java.lang.Exception
HEAD
request.queryData
- Request headers. Replaces all the values set
by setFormData(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse HEAD(java.lang.String uri, NVPair[] queryData) throws java.lang.Exception
HEAD
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.queryData
- Request headers. Replaces all the values set
by setFormData(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse HEAD(java.lang.String uri, NVPair[] queryData, NVPair[] headers) throws java.lang.Exception
HEAD
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.queryData
- Request headers. Replaces all the values set by
setFormData(HTTPClient.NVPair[])
.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS() throws java.lang.Exception
OPTIONS
request.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS(java.lang.String uri) throws java.lang.Exception
OPTIONS
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS(java.lang.String uri, byte[] data) throws java.lang.Exception
OPTIONS
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.data
- Data to be submitted in the body of the request.
Overrides the value set with setData(byte[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS(java.lang.String uri, byte[] data, NVPair[] headers) throws java.lang.Exception
OPTIONS
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.data
- Data to be submitted in the body of the request. Overrides the
value set with setData(byte[])
.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS(java.lang.String uri, java.io.InputStream inputStream) throws java.lang.Exception
OPTIONS
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.java.lang.Exception
- If an error occurs.public final HTTPResponse OPTIONS(java.lang.String uri, java.io.InputStream inputStream, NVPair[] headers) throws java.lang.Exception
OPTIONS
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse POST() throws java.lang.Exception
POST
request.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(NVPair[] formData) throws java.lang.Exception
POST
request.formData
- Data to be submitted as an
application/x-www-form-urlencoded
encoded request
body.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, NVPair[] formData) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.formData
- Data to be submitted as an
application/x-www-form-urlencoded
encoded request
body.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, NVPair[] formData, NVPair[] headers) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.formData
- Data to be submitted as an
application/x-www-form-urlencoded
encoded request
body.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, NVPair[] formData, NVPair[] headers, boolean isMultipart) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.formData
- Data to be submitted as an
application/x-www-form-urlencoded
or
multipart/form-data
encoded request
body.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.isMultipart
- true
if request type is multipart/form-data.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, byte[] data) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.data
- Data to be submitted in the body of the request.
Overrides the value set with setData(byte[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, byte[] data, NVPair[] headers) throws java.lang.Exception
POST
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.data
- Data to be submitted in the body of the request.
Overrides the value set with setData(byte[])
.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, java.io.InputStream inputStream) throws java.lang.Exception
POST
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.java.lang.Exception
- If an error occurs.public final HTTPResponse POST(java.lang.String uri, java.io.InputStream inputStream, NVPair[] headers) throws java.lang.Exception
POST
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT() throws java.lang.Exception
PUT
request.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT(java.lang.String uri) throws java.lang.Exception
PUT
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT(java.lang.String uri, byte[] data) throws java.lang.Exception
PUT
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.data
- Data to be submitted in the body of the request.
Overrides the value set with setData(byte[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT(java.lang.String uri, byte[] data, NVPair[] headers) throws java.lang.Exception
PUT
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.data
- Data to be submitted in the body of the request. Overrides the
value set with setData(byte[])
.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT(java.lang.String uri, java.io.InputStream inputStream) throws java.lang.Exception
PUT
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.java.lang.Exception
- If an error occurs.public final HTTPResponse PUT(java.lang.String uri, java.io.InputStream inputStream, NVPair[] headers) throws java.lang.Exception
PUT
request. This version allows the data
to be passed as a stream, see the note in the
class description
.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.inputStream
- Data to be submitted in the body of the request.
This stream will be fully read and closed when the method is called.
The value set with setData(byte[])
is ignored.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.public final HTTPResponse TRACE() throws java.lang.Exception
TRACE
request.java.lang.Exception
- If an error occurs.public final HTTPResponse TRACE(java.lang.String uri) throws java.lang.Exception
TRACE
request.uri
- The URI. If a default URL has been specified with
setUrl(java.lang.String)
, this value need not be absolute and, if
relative, it will be resolved relative to the default URL.
Otherwise this value must be an absolute URL.java.lang.Exception
- If an error occurs.public final HTTPResponse TRACE(java.lang.String uri, NVPair[] headers) throws java.lang.Exception
TRACE
request.uri
- The URI. If a default URL has been specified with setUrl(java.lang.String)
,
this value need not be absolute and, if relative, it will be
resolved relative to the default URL. Otherwise this value must be
an absolute URL.headers
- Request headers. Overrides headers with matching names set by
setHeaders(HTTPClient.NVPair[])
.java.lang.Exception
- If an error occurs.protected void processResponse(HTTPResponse response)
response
- The response.protected PluginProcessContext getPluginProcessContext()
public static Test.InstrumentationFilter getHttpMethodFilter()
DELETE
, GET
, and so on.