public class HttpRequest extends java.lang.Object implements IHttpRequest
IHttpRequest.HttpCommand, IHttpRequest.HttpHeader, IHttpRequest.HttpResponseStatus, IHttpRequest.Key| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CHARSET_EQUAL
'charset=', it is used to describe the content charset in
HttpHeader#CONTENT_TYPE |
static java.lang.String |
CONTENT_TYPE_APPLICATION_FORM_URLENCODED
'application/x-www-form-urlencoded', it is a content type, refer to its definition
|
protected java.lang.String |
contentCharset
The charset to set to the HTTP header
HttpHeader#CONTENT_TYPE, the default value is StringUtils.CHARSET_UTF8 |
protected java.lang.String |
contentLauguage
The value to set to the HTTP header
HttpHeader#CONTENT_LANGUAGE, the default value is Locale.US |
protected java.lang.String |
contentType
The value to set to the HTTP header
HttpHeader#CONTENT_TYPE, the default value is CONTENT_TYPE_APPLICATION_FORM_URLENCODED. |
static boolean |
DEFAULT_INCLUDE_HEARERS
false
|
protected boolean |
includeRequestHeaders
A boolean indicating whether or not to include request headers information in the response.
|
protected boolean |
includeResponseHeaders
A boolean indicating whether or not to include response headers information in the response.
|
protected boolean |
useCaches
A boolean indicating whether or not to allow caching.
|
| Constructor and Description |
|---|
HttpRequest() |
| Modifier and Type | Method and Description |
|---|---|
java.util.Map<java.lang.String,java.lang.Object> |
execute(IHttpRequest.HttpCommand command,
java.lang.String url,
boolean async,
java.util.Map<java.lang.String,java.lang.String> requestHeaders,
java.lang.String content)
Send a simple HTTP request.
If a PROXY server is needed to visit a site, we need to specify the PROXY server as JVM parameter, such as: -Dhttp.proxyHost=proxy.host.name -Dhttp.proxyPort=portNumber -Dhttps.proxyHost=proxy.host.name -Dhttps.proxyPort=portNumber Besides the keys mentioned IHttpRequest.execute(org.safs.net.IHttpRequest.HttpCommand, String, boolean, Map, String), the returned Map may also contain Key#REQUEST_HEADERS. |
static java.lang.String |
execute(java.lang.String command,
java.lang.String url,
java.lang.String content,
java.lang.String... headerValuePairs)
Deprecated.
Please use
execute(org.safs.net.IHttpRequest.HttpCommand, String, boolean, Map, String) instead. |
java.lang.String |
getContentCharset()
getter for
contentCharset |
java.lang.String |
getContentLauguage()
getter for
contentLauguage |
java.lang.String |
getContentType()
getter for
contentType |
java.lang.String |
getHttpStatus()
After executing HTTP request, call this method to get the value of
IHttpRequest.Key.RESPONSE_STATUS. |
java.lang.String |
getHttpStatusText()
After executing HTTP request, call this method to get the value of
IHttpRequest.Key.RESPONSE_STATUS_TEXT. |
java.lang.Object |
getResponseHeaders()
After executing HTTP request, call this method to get the value of
IHttpRequest.Key.RESPONSE_HEADERS. |
java.lang.String |
getResponseText()
After executing HTTP request, call this method to get the value of
IHttpRequest.Key.RESPONSE_TEXT. |
java.lang.Object |
getResponseXml()
After executing HTTP request, call this method to get the value of
IHttpRequest.Key.RESPONSE_XML. |
static java.lang.String |
getUsage() |
boolean |
isIncludeRequestHeaders()
getter for
includeRequestHeaders |
boolean |
isIncludeResponseHeaders()
setter for
includeResponseHeaders |
boolean |
isUseCaches()
getter for
useCaches |
static void |
main(java.lang.String[] args)
Send a simple HTTP request.
If a PROXY server is needed to visit web site, we need to specify the PROXY server as JVM parameter, such as: -Dhttp.proxyHost=proxy.host.name -Dhttp.proxyPort=portNumber |
void |
setContentCharset(java.lang.String contentCharset)
setter for
contentCharset |
void |
setContentLauguage(java.lang.String contentLauguage)
setter for
contentLauguage |
void |
setContentType(java.lang.String contentType)
setter for
contentType |
void |
setIncludeRequestHeaders(boolean includeRequestHeaders)
setter for
includeRequestHeaders |
void |
setIncludeResponseHeaders(boolean includeResponseHeaders)
setter for
includeResponseHeaders |
void |
setUseCaches(boolean useCaches)
setter for
useCaches |
public static final java.lang.String CONTENT_TYPE_APPLICATION_FORM_URLENCODED
public static final java.lang.String CHARSET_EQUAL
HttpHeader#CONTENT_TYPEpublic static final boolean DEFAULT_INCLUDE_HEARERS
protected java.lang.String contentType
HttpHeader#CONTENT_TYPE, the default value is CONTENT_TYPE_APPLICATION_FORM_URLENCODED.protected java.lang.String contentCharset
HttpHeader#CONTENT_TYPE, the default value is StringUtils.CHARSET_UTF8protected java.lang.String contentLauguage
HttpHeader#CONTENT_LANGUAGE, the default value is Locale.USprotected boolean useCaches
protected boolean includeRequestHeaders
protected boolean includeResponseHeaders
public java.lang.String getContentType()
contentTypepublic java.lang.String getContentCharset()
contentCharsetpublic java.lang.String getContentLauguage()
contentLauguagepublic boolean isUseCaches()
useCachespublic boolean isIncludeRequestHeaders()
includeRequestHeaderspublic boolean isIncludeResponseHeaders()
includeResponseHeaderspublic void setContentType(java.lang.String contentType)
contentTypepublic void setContentCharset(java.lang.String contentCharset)
contentCharsetpublic void setContentLauguage(java.lang.String contentLauguage)
contentLauguagepublic void setUseCaches(boolean useCaches)
useCachespublic void setIncludeRequestHeaders(boolean includeRequestHeaders)
includeRequestHeaderspublic void setIncludeResponseHeaders(boolean includeResponseHeaders)
includeResponseHeaderspublic static java.lang.String execute(java.lang.String command,
java.lang.String url,
java.lang.String content,
java.lang.String... headerValuePairs)
throws java.lang.Exception
execute(org.safs.net.IHttpRequest.HttpCommand, String, boolean, Map, String) instead.command - String, the HTTP request command, it can be one of CommandHttp.url - String, the HTTP URL accepting HTTP requestcontent - String, the content to send for this HTTP request.headerValuePairs - String[], the pair of (header,value) for HTTP request, the header key can be one of HeaderHttpjava.lang.Exception - when there are some error occurs.public java.util.Map<java.lang.String,java.lang.Object> execute(IHttpRequest.HttpCommand command, java.lang.String url, boolean async, java.util.Map<java.lang.String,java.lang.String> requestHeaders, java.lang.String content) throws SAFSException
IHttpRequest.execute(org.safs.net.IHttpRequest.HttpCommand, String, boolean, Map, String), the returned Map may also contain Key#REQUEST_HEADERS.execute in interface IHttpRequestcommand - HttpCommand, the method to execute, such as 'GET', 'POST', 'PUT' etc.url - String, the URL to requestasync - boolean, if the HTTP request will be executed asynchronously.requestHeaders - Mapcontent - String, the data to send with the request.IHttpRequest.Key.READY_STATE: ready state, changes from 0 to 4, ONLY for AJAX XMLHttpRequest execution.
IHttpRequest.Key.RESPONSE_STATUS: HTTP response status number
IHttpRequest.Key.RESPONSE_STATUS_TEXT: HTTP response status text
IHttpRequest.Key.RESPONSE_HEADERS: HTTP response headers
IHttpRequest.Key.RESPONSE_TEXT: HTTP response as string
IHttpRequest.Key.RESPONSE_XML: HTTP response as XML data
IHttpRequest.Key.READY_STATE: ready state, changes from 0 to 4, ONLY for AJAX XMLHttpRequest execution. for other values, they are not in the Map result, we can get them from following methods:#getReadyState()IHttpRequest.getHttpStatus()IHttpRequest.getHttpStatusText()IHttpRequest.getResponseText()IHttpRequest.getResponseXml()IHttpRequest.getResponseHeaders()
SAFSException - when there are some error occurs.IHttpRequest.execute(org.safs.net.IHttpRequest.HttpCommand, String, boolean, Map, String)public static java.lang.String getUsage()
public static void main(java.lang.String[] args)
args - String[], the parameters for HTTP Request
args[0] String, the HTTP request command, it can be one of enumeration HttpCommand.
args[1] String, the HTTP URL accepting this HTTP request.
args[2] String, the content to send for this HTTP request, it can be empty string.
from args[3], pairs of (header-key, value) will be provided to set headers for HTTP request
for example, args[3] is "Accept", args[4] is "text/plain, text/html"
args[5] is "Expires", args[6] is "Thu, 01 Dec 1994 16:00:00 GMT"
java org.safs.net.HttpRequest "GET" "https://www.google.com" java org.safs.net.HttpRequest "HEAD" "https://www.google.com" java org.safs.net.HttpRequest "GET" "http://www.w3schools.com/html/action_page.php?firstname=Mickey&lastname=Mouse" java org.safs.net.HttpRequest "POST" "http://www.w3schools.com/php/welcome.php" "name=Mickey Mouse&email=mickey.mouse@disneyland.com"
public java.lang.Object getResponseHeaders()
IHttpRequestIHttpRequest.Key.RESPONSE_HEADERS.
This is more useful for asynchronous execution; for synchronous execution the Map result contains this value.getResponseHeaders in interface IHttpRequestIHttpRequest.Key.RESPONSE_HEADERS.IHttpRequest.execute(HttpCommand, String, boolean, Map, String)public java.lang.String getResponseText()
IHttpRequestIHttpRequest.Key.RESPONSE_TEXT.
This is more useful for asynchronous execution; for synchronous execution the Map result contains this value.getResponseText in interface IHttpRequestIHttpRequest.Key.RESPONSE_TEXT.IHttpRequest.execute(HttpCommand, String, boolean, Map, String)public java.lang.Object getResponseXml()
IHttpRequestIHttpRequest.Key.RESPONSE_XML.
This is more useful for asynchronous execution; for synchronous execution the Map result contains this value.getResponseXml in interface IHttpRequestIHttpRequest.Key.RESPONSE_XML.IHttpRequest.execute(HttpCommand, String, boolean, Map, String)public java.lang.String getHttpStatus()
IHttpRequestIHttpRequest.Key.RESPONSE_STATUS.
This is more useful for asynchronous execution; for synchronous execution the Map result contains this value.getHttpStatus in interface IHttpRequestIHttpRequest.Key.RESPONSE_STATUS.IHttpRequest.execute(HttpCommand, String, boolean, Map, String)public java.lang.String getHttpStatusText()
IHttpRequestIHttpRequest.Key.RESPONSE_STATUS_TEXT.
This is more useful for asynchronous execution; for synchronous execution the Map result contains this value.getHttpStatusText in interface IHttpRequestIHttpRequest.Key.RESPONSE_STATUS_TEXT.IHttpRequest.execute(HttpCommand, String, boolean, Map, String)Copyright © SAS Institute. All Rights Reserved.