public class HttpRequest
extends java.lang.Object
A cross-platform HTTP API modeled after XMLHttpRequest. Supports streaming of request and response content.
public void classExample() { try { com.sap.client.odata.v4.core.DebugConsole.traceIn("example.HttpRequestExample.classExample"); { com.sap.client.odata.v4.http.HttpRequest request = new com.sap.client.odata.v4.http.HttpRequest(); request.open(com.sap.client.odata.v4.http.HttpRequest.GET, "http://google.com"); request.send(); String text = request.getResponseText(); request.close(); Example.show("google.com home page text: ", text); } } catch (java.lang.RuntimeException _tex_) { com.sap.client.odata.v4.core.Assert.error("/data/home/ppurple/xMDP/workspace/NAAS-Mobile-com.sap.odata.core.framework-OD-darwinintel64_ent_indirectshipment-darwinintel64_ent/src/main/xs/examples/example.HttpRequestExample.xs:5:11", _tex_); throw _tex_; } finally { com.sap.client.odata.v4.core.DebugConsole.traceOut("example.HttpRequestExample.classExample"); } }
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CONNECT
The HTTP CONNECT verb.
|
static java.lang.String |
DELETE
The HTTP DELETE verb.
|
static java.lang.String |
GET
The HTTP GET verb.
|
static java.lang.String |
HEAD
The HTTP HEAD verb.
|
static java.lang.String |
OPTIONS
The HTTP OPTIONS verb.
|
static java.lang.String |
PATCH
The HTTP PATCH verb.
|
static java.lang.String |
POST
The HTTP POST verb.
|
static java.lang.String |
PUT
The HTTP PUT verb.
|
static java.lang.String |
TRACE
The HTTP TRACE verb.
|
Constructor and Description |
---|
HttpRequest() |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this HTTP request, and any response stream that may have been opened by
HttpRequest.send . |
void |
enableTrace(java.lang.String serviceName,
boolean traceRequest,
boolean traceHeaders,
boolean traceContent,
boolean prettyPrint)
Enable HTTP request tracing.
|
boolean |
getCompressResponse()
Set to
false before calling HttpRequest.send if the caller does not wish the server to compress the response content (using "gzip"). |
HttpHandler |
getHandler()
(nullable) The optional internal handler for this request.
|
java.lang.String |
getMethod()
The HTTP method for this request.
|
java.lang.String |
getPassword()
(nullable) The password for this request.
|
boolean |
getPrettyPrint()
Is this request enabled for pretty-printed tracing?
|
ByteStream |
getRequestBytes()
(nullable) Request data stream.
|
CharStream |
getRequestChars()
(nullable) Request text stream.
|
java.lang.String |
getRequestCookie(java.lang.String name)
Get a request cookie value.
|
HttpCookies |
getRequestCookies()
HTTP request cookies.
|
byte[] |
getRequestData()
(nullable) Request data for a non-streamed request.
|
java.lang.String |
getRequestHeader(java.lang.String name)
Get a request header value.
|
HttpHeaders |
getRequestHeaders()
HTTP request headers.
|
java.lang.String |
getRequestText()
(nullable) Request text for a non-streamed request.
|
ByteStream |
getResponseBytes()
Response data stream.
|
CharStream |
getResponseChars()
Response text stream.
|
java.lang.String |
getResponseCookie(java.lang.String name)
Get a response cookie value.
|
HttpCookies |
getResponseCookies()
HTTP response cookies.
|
byte[] |
getResponseData()
Response data for a non-streamed response.
|
long |
getResponseDataCount()
Response data counter.
|
long |
getResponseGzipCount()
Response gzip counter.
|
java.lang.String |
getResponseHeader(java.lang.String name)
Get a response header value.
|
HttpHeaders |
getResponseHeaders()
HTTP response headers.
|
java.lang.String |
getResponseText()
Response text for a non-streamed response.
|
int |
getStatus()
HTTP response status code.
|
java.lang.String |
getStatusText()
HTTP response status text.
|
boolean |
getStreamRequest()
Set to
false before calling HttpRequest.send if the caller does not wish to stream the request content. |
boolean |
getStreamResponse()
Set to
false before calling HttpRequest.send if the caller does not wish to stream the response content. |
boolean |
getTraceContent()
Is this request enabled for tracing of request/response content?
|
boolean |
getTraceHeaders()
Is this request enabled for tracing of request/response headers?
|
boolean |
getTraceRequest()
Is this request enabled for request/response tracing?
|
boolean |
getUnzipResponse()
Does response need to be unzipped? Should be set by
HttpHandler subclasses. |
java.lang.String |
getUrl()
The URL for this request.
|
java.lang.String |
getUsername()
(nullable) The username for this request.
|
int |
getVersion()
The HTTP version for this request.
|
boolean |
hasResponseBytes()
Does this request have a byte stream response already set?
|
boolean |
hasResponseChars()
Does this request have a text stream response already set?
|
boolean |
hasResponseData()
Does this request have a binary (data) response already set?
|
boolean |
hasResponseText()
Does this request have a string (text) response already set?
|
boolean |
isActive()
Is a connection active (
HttpRequest.send was called, but HttpRequest.close has not yet been called)? |
void |
login(java.lang.String username,
java.lang.String password)
Specify the HTTP basic credentials.
|
void |
open(java.lang.String method,
java.lang.String url)
Specify the HTTP
HttpRequest.method and HttpRequest.url . |
void |
send()
Send this HTTP request to the server, and wait for a response.
|
void |
setActive(boolean value)
Is a connection active (
HttpRequest.send was called, but HttpRequest.close has not yet been called)? |
void |
setCompressResponse(boolean value)
Set to
false before calling HttpRequest.send if the caller does not wish the server to compress the response content (using "gzip"). |
void |
setHandler(HttpHandler value)
The optional internal handler for this request.
|
void |
setInputStream(java.io.InputStream stream) |
void |
setMethod(java.lang.String value)
The HTTP method for this request.
|
void |
setPassword(java.lang.String value)
The password for this request.
|
void |
setRequestBytes(ByteStream value)
Request data stream.
|
void |
setRequestChars(CharStream value)
Request text stream.
|
void |
setRequestCookie(java.lang.String name,
java.lang.String value)
Set a request cookie.
|
void |
setRequestCookies(HttpCookies value)
HTTP request cookies.
|
void |
setRequestData(byte[] value)
Request data for a non-streamed request.
|
void |
setRequestHeader(java.lang.String name,
java.lang.String value)
Set a request header.
|
void |
setRequestHeaders(HttpHeaders value)
HTTP request headers.
|
void |
setRequestText(java.lang.String value)
Request text for a non-streamed request.
|
void |
setResponseBytes(ByteStream value)
Response data stream.
|
void |
setResponseChars(CharStream value)
Response text stream.
|
void |
setResponseCookie(java.lang.String name,
java.lang.String value)
Set a response cookie.
|
void |
setResponseCookies(HttpCookies value)
HTTP response cookies.
|
void |
setResponseData(byte[] value)
Response data for a non-streamed response.
|
void |
setResponseHeader(java.lang.String name,
java.lang.String value)
Set a response header.
|
void |
setResponseHeaders(HttpHeaders value)
HTTP response headers.
|
void |
setResponseText(java.lang.String value)
Response text for a non-streamed response.
|
void |
setStatus(int value)
HTTP response status code.
|
void |
setStatusText(java.lang.String value)
HTTP response status text.
|
void |
setStreamRequest(boolean value)
Set to
false before calling HttpRequest.send if the caller does not wish to stream the request content. |
void |
setStreamResponse(boolean value)
Set to
false before calling HttpRequest.send if the caller does not wish to stream the response content. |
void |
setUnzipResponse(boolean value)
Does response need to be unzipped? Should be set by
HttpHandler subclasses. |
void |
setUrl(java.lang.String value)
The URL for this request.
|
void |
setUsername(java.lang.String value)
The username for this request.
|
void |
setVersion(int value)
The HTTP version for this request.
|
public static final java.lang.String CONNECT
The HTTP CONNECT verb.
public static final java.lang.String DELETE
The HTTP DELETE verb.
public static final java.lang.String GET
The HTTP GET verb.
public static final java.lang.String HEAD
The HTTP HEAD verb.
public static final java.lang.String OPTIONS
The HTTP OPTIONS verb.
public static final java.lang.String PATCH
The HTTP PATCH verb.
public static final java.lang.String POST
The HTTP POST verb.
public static final java.lang.String PUT
The HTTP PUT verb.
public static final java.lang.String TRACE
The HTTP TRACE verb.
public void setInputStream(java.io.InputStream stream)
public void close()
Close this HTTP request, and any response stream that may have been opened by HttpRequest.send
.
If an application calls HttpRequest.send
, then it must also call this function to avoid resource leaks.
public void enableTrace(java.lang.String serviceName, boolean traceRequest, boolean traceHeaders, boolean traceContent, boolean prettyPrint)
Enable HTTP request tracing. Call this before calling HttpRequest.send
.
serviceName
- (nullable) Name of the service which this request will be sent to.traceRequest
- Trace the request (URL), and basic request/response flow. Note: this may result in tracing of sensitive data, such as URL query parameters.traceHeaders
- Trace the headers. Note: this may result in tracing of sensitive data, such as authentication information or cookie values.traceContent
- Trace the content. Note: this may result in tracing of sensitive data.prettyPrint
- If traceContent
is true
and HttpRequest.streamResponse
is false
, then content tracing of JSON or XML will be pretty-printed.public boolean getCompressResponse()
Set to false
before calling HttpRequest.send
if the caller does not wish the server to compress the response content (using "gzip").
True by default.
public HttpHandler getHandler()
(nullable) The optional internal handler for this request.
public java.lang.String getMethod()
The HTTP method for this request. Set this using the HttpRequest.open
function.
public java.lang.String getPassword()
(nullable) The password for this request. Set this using the HttpRequest.login
function.
public boolean getPrettyPrint()
Is this request enabled for pretty-printed tracing?
public ByteStream getRequestBytes()
(nullable) Request data stream. Set this before calling HttpRequest.send
.
public CharStream getRequestChars()
(nullable) Request text stream. Set this before calling HttpRequest.send
.
public java.lang.String getRequestCookie(java.lang.String name)
Get a request cookie value. For use by servers.
name
- Cookie name.public HttpCookies getRequestCookies()
HTTP request cookies. Set request cookies before calling HttpRequest.send
.
public byte[] getRequestData()
(nullable) Request data for a non-streamed request. Set this before calling HttpRequest.send
.
public java.lang.String getRequestHeader(java.lang.String name)
Get a request header value. For use by servers.
name
- Header name.public HttpHeaders getRequestHeaders()
HTTP request headers. Set request headers before calling HttpRequest.send
.
public java.lang.String getRequestText()
(nullable) Request text for a non-streamed request. Set this before calling HttpRequest.send
.
public ByteStream getResponseBytes()
Response data stream. Access this after calling HttpRequest.send
.
public CharStream getResponseChars()
Response text stream. Access this after calling HttpRequest.send
.
public java.lang.String getResponseCookie(java.lang.String name)
Get a response cookie value. Call this after calling HttpRequest.send
.
name
- Cookie name.public HttpCookies getResponseCookies()
HTTP response cookies. Access response cookies after calling HttpRequest.send
.
public byte[] getResponseData()
Response data for a non-streamed response. Access this after calling HttpRequest.send
.
public long getResponseDataCount()
Response data counter. Counts received content data length in bytes.
public long getResponseGzipCount()
Response gzip counter. Counts received content gzip length in bytes.
public java.lang.String getResponseHeader(java.lang.String name)
Get a response header value. Call this after calling HttpRequest.send
.
name
- Header name.public HttpHeaders getResponseHeaders()
HTTP response headers. Access response headers after calling HttpRequest.send
.
public java.lang.String getResponseText()
Response text for a non-streamed response. Access this after calling HttpRequest.send
.
public int getStatus()
HTTP response status code. Access this after calling HttpRequest.send
.
public java.lang.String getStatusText()
HTTP response status text. Access this after calling HttpRequest.send
.
public boolean getStreamRequest()
Set to false
before calling HttpRequest.send
if the caller does not wish to stream the request content.
True by default.
public boolean getStreamResponse()
Set to false
before calling HttpRequest.send
if the caller does not wish to stream the response content.
True by default.
public boolean getTraceContent()
Is this request enabled for tracing of request/response content?
public boolean getTraceHeaders()
Is this request enabled for tracing of request/response headers?
public boolean getTraceRequest()
Is this request enabled for request/response tracing?
public boolean getUnzipResponse()
Does response need to be unzipped? Should be set by HttpHandler
subclasses.
public java.lang.String getUrl()
The URL for this request. Set this using the HttpRequest.open
function.
public java.lang.String getUsername()
(nullable) The username for this request. Set this using the HttpRequest.login
function.
public int getVersion()
The HTTP version for this request. Defaults to HTTP 1.1.
public boolean hasResponseBytes()
Does this request have a byte stream response already set?
public boolean hasResponseChars()
Does this request have a text stream response already set?
public boolean hasResponseData()
Does this request have a binary (data) response already set?
public boolean hasResponseText()
Does this request have a string (text) response already set?
public boolean isActive()
Is a connection active (HttpRequest.send
was called, but HttpRequest.close
has not yet been called)?
public void login(java.lang.String username, java.lang.String password)
Specify the HTTP basic credentials. Call this before calling HttpRequest.send
if the server requires HTTP basic authentication.
username
- Username for HTTP basic authentication.password
- Password for HTTP basic authentication.public void open(java.lang.String method, java.lang.String url)
Specify the HTTP HttpRequest.method
and HttpRequest.url
. Call this before calling HttpRequest.send
.
method
- Value for HttpRequest.method
.url
- Value for HttpRequest.url
.public void send()
Send this HTTP request to the server, and wait for a response.
If HttpRequest.streamResponse
is true
, then waiting is only until response headers come back, and response content may be streamed as it is accessed.
If an application calls this function, then it must also call HttpRequest.close
after accessing all desired response content, to avoid resource leaks.
public void setActive(boolean value)
Is a connection active (HttpRequest.send
was called, but HttpRequest.close
has not yet been called)?
public void setCompressResponse(boolean value)
Set to false
before calling HttpRequest.send
if the caller does not wish the server to compress the response content (using "gzip").
True by default.
public void setHandler(HttpHandler value)
The optional internal handler for this request.
public void setMethod(java.lang.String value)
The HTTP method for this request. Set this using the HttpRequest.open
function.
public void setPassword(java.lang.String value)
The password for this request. Set this using the HttpRequest.login
function.
public void setRequestBytes(ByteStream value)
Request data stream. Set this before calling HttpRequest.send
.
public void setRequestChars(CharStream value)
Request text stream. Set this before calling HttpRequest.send
.
public void setRequestCookie(java.lang.String name, java.lang.String value)
Set a request cookie. Call this before calling HttpRequest.send
.
name
- Cookie name.value
- Cookie value.public void setRequestCookies(HttpCookies value)
HTTP request cookies. Set request cookies before calling HttpRequest.send
.
public void setRequestData(byte[] value)
Request data for a non-streamed request. Set this before calling HttpRequest.send
.
public void setRequestHeader(java.lang.String name, java.lang.String value)
Set a request header. Call this before calling HttpRequest.send
.
name
- Header name.value
- Header value.public void setRequestHeaders(HttpHeaders value)
HTTP request headers. Set request headers before calling HttpRequest.send
.
public void setRequestText(java.lang.String value)
Request text for a non-streamed request. Set this before calling HttpRequest.send
.
public void setResponseBytes(ByteStream value)
Response data stream. Access this after calling HttpRequest.send
.
public void setResponseChars(CharStream value)
Response text stream. Access this after calling HttpRequest.send
.
public void setResponseCookie(java.lang.String name, java.lang.String value)
Set a response cookie. For use by servers.
name
- Cookie name.value
- Cookie value.public void setResponseCookies(HttpCookies value)
HTTP response cookies. Access response cookies after calling HttpRequest.send
.
public void setResponseData(byte[] value)
Response data for a non-streamed response. Access this after calling HttpRequest.send
.
public void setResponseHeader(java.lang.String name, java.lang.String value)
Set a response header. For use by servers.
name
- Header name.value
- Header value.public void setResponseHeaders(HttpHeaders value)
HTTP response headers. Access response headers after calling HttpRequest.send
.
public void setResponseText(java.lang.String value)
Response text for a non-streamed response. Access this after calling HttpRequest.send
.
public void setStatus(int value)
HTTP response status code. Access this after calling HttpRequest.send
.
public void setStatusText(java.lang.String value)
HTTP response status text. Access this after calling HttpRequest.send
.
public void setStreamRequest(boolean value)
Set to false
before calling HttpRequest.send
if the caller does not wish to stream the request content.
True by default.
public void setStreamResponse(boolean value)
Set to false
before calling HttpRequest.send
if the caller does not wish to stream the response content.
True by default.
public void setUnzipResponse(boolean value)
Does response need to be unzipped? Should be set by HttpHandler
subclasses.
public void setUrl(java.lang.String value)
The URL for this request. Set this using the HttpRequest.open
function.
public void setUsername(java.lang.String value)
The username for this request. Set this using the HttpRequest.login
function.
public void setVersion(int value)
The HTTP version for this request. Defaults to HTTP 1.1.