Class HttpResponse
- All Implemented Interfaces:
AutoCloseable
Inspect HTTP status, headers, cookies, redirects, and response bodies.
Chilkat.HttpResponse represents an HTTP response returned by
certain Chilkat.Http methods. It provides access to the response
status line, status code, headers, cookies, charset, content length, body
bytes, body text, full MIME response, redirect URL, and helper methods for
saving or loading the response body into Chilkat objects such as
BinData, StringBuilder, JsonObject,
JsonArray, and Xml.
Status and reason
Read the HTTP status code, status line, and related response information to determine whether the request succeeded, redirected, or failed.
Headers and cookies
Inspect response headers, retrieve individual header values, and access cookies returned by the server.
Body as text or bytes
Get the response body as raw bytes or decoded text, using the response charset information when appropriate.
Structured body loading
Load the body directly into JsonObject,
JsonArray, Xml, BinData, or
StringBuilder objects for further processing.
Redirect information
Access redirect-related information such as the redirect URL when the server responds with a redirect status.
Save and inspect responses
Save body content, inspect the full MIME-style response, and use response metadata for diagnostics, logging, or API troubleshooting.
Common pattern:
Send a request with Chilkat.Http, check the
StatusCode, inspect headers or cookies as needed, then retrieve
the body in the form best suited to the response content: text, bytes,
BinData, StringBuilder, JSON, or XML. Use
HttpResponse for response inspection; use Chilkat.Http
for the actual network request.
For an extended overview, see HttpResponse Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCopies the response body to a BinData object.voidgetBodyJarr(JsonArray jarr)Loads the JSON array response body into a JsonArray This method should only be called when it is known that the response body contains a JSON array (beginning with a square bracket '[' and ending with a square bracket ']')voidgetBodyJson(JsonObject json)Loads the JSON response body into a JsonObject.The same as the Body property, but returned as a quoted-printable encoded string.voidgetBodySb(StringBuilder sb)Copies the response body to a Chilkat StringBuilder object.The response body returned as a string.voidgetBodyXml(Xml xml)Loads the XML response body into an XML object.The response charset, such asiso-8859-1, if applicable.longThe content length of the response, in bytes.longThe content length of the response, in number of bytes, returned as a 64-bit integer.getCookieDomain(int index)Returns the domain of the Nth cookie in the response.getCookieExpiresStr(int index)Returns the expiration date/time of the Nth cookie in the response.getCookieName(int index)Returns the name of the Nth cookie returned in the response.getCookiePath(int index)Returns the path of the Nth cookie returned in the response.getCookieValue(int index)Returns the value of the Nth cookie returned in the response.Returns the content of the Date response header field in RFC822 date/time string format.The domain of the HTTP server that created this response.Returns the redirect URL for 301/302 responses.Returns the full MIME (header + body) of the HTTP response.The full text of the response header.getHeaderField(String fieldName)Returns the value of a response header field accessed by field name.getHeaderFieldAttr(String fieldName, String attrName)Returns a response header field attribute.getHeaderName(int index)Gets the name of the Nth response header field.getHeaderValue(int index)Gets the value of the Nth response header field.intThe number of cookies included in the response.intThe number of response header fields.intThe status code (as an integer) from the first line of an HTTP response.The first line of an HTTP response, such asHTTP/1.0 200 OK.The text that follows the status code in the 1st line of the HTTP response.This is a catch-all property to be used for uncommon needs.voidsaveBodyBinary(String path)Saves the body of the HTTP response to a file.voidsaveBodyText(boolean bCrlf, String path)Saves the HTTP response body to a file.voidsetUncommonOptions(String value)Sets theUncommonOptionsproperty; seegetUncommonOptions().urlEncParamValue(String encodedParamString, String paramName)Convenience method for parsing a param's value out of a URL-encoded param string.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
HttpResponse
public HttpResponse()Creates a newHttpResponseobject.
-
-
Method Details
-
getBodyQP
The same as the Body property, but returned as a quoted-printable encoded string.- Returns:
- the value of the
BodyQPproperty (read-only)
-
getBodyStr
The response body returned as a string.- Returns:
- the value of the
BodyStrproperty (read-only)
-
getCharset
The response charset, such asiso-8859-1, if applicable. Obviously, responses for GIF and JPG files will not have a charset.- Returns:
- the value of the
Charsetproperty (read-only)
-
getContentLength
public long getContentLength()The content length of the response, in bytes.- Returns:
- the value of the
ContentLengthproperty (read-only)
-
getContentLength64
public long getContentLength64()The content length of the response, in number of bytes, returned as a 64-bit integer.- Returns:
- the value of the
ContentLength64property (read-only)
-
getDateStr
Returns the content of the Date response header field in RFC822 date/time string format.- Returns:
- the value of the
DateStrproperty (read-only)
-
getDomain
The domain of the HTTP server that created this response.- Returns:
- the value of the
Domainproperty (read-only)
-
getFinalRedirectUrl
Returns the redirect URL for 301/302 responses.- Returns:
- the value of the
FinalRedirectUrlproperty (read-only)
-
getFullMime
Returns the full MIME (header + body) of the HTTP response.- Returns:
- the value of the
FullMimeproperty (read-only)
-
getHeader
The full text of the response header.- Returns:
- the value of the
Headerproperty (read-only)
-
getNumCookies
public int getNumCookies()The number of cookies included in the response.- Returns:
- the value of the
NumCookiesproperty (read-only)
-
getNumHeaderFields
public int getNumHeaderFields()The number of response header fields.- Returns:
- the value of the
NumHeaderFieldsproperty (read-only)
-
getStatusCode
public int getStatusCode()The status code (as an integer) from the first line of an HTTP response. If the StatusLine =HTTP/1.0 200 OK, the response status code returned is 200.- Returns:
- the value of the
StatusCodeproperty (read-only)
-
getStatusLine
The first line of an HTTP response, such asHTTP/1.0 200 OK.- Returns:
- the value of the
StatusLineproperty (read-only)
-
getStatusText
The text that follows the status code in the 1st line of the HTTP response. For example, i the first line of an HTTP response isHTTP/1.0 200 OK, then this property containsOK.- Returns:
- the value of the
StatusTextproperty (read-only)
-
getUncommonOptions
This is a catch-all property to be used for uncommon needs. Can be set to a list of the following comma separated keywords:TakeResponseBody- Introduced in v9.5.0.89. The GetBody* methods will transfer the body to the target object instead of copying the contents. This can be useful if the response body is extremely large because it will reduce the overall memory requirement.
- Returns:
- the value of the
UncommonOptionsproperty
-
setUncommonOptions
Sets theUncommonOptionsproperty; seegetUncommonOptions().- Parameters:
value- the new value (notnull)
-
getBodyBd
Copies the response body to a BinData object.- Parameters:
binData- thebinDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getBodyJarr
Loads the JSON array response body into a JsonArray This method should only be called when it is known that the response body contains a JSON array (beginning with a square bracket '[' and ending with a square bracket ']')- Parameters:
jarr- thejarrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getBodyJson
Loads the JSON response body into a JsonObject. This method should only be called when it is known that the response body contains JSON (beginning with a curly brace '{' and ending with a curly brace '}')- Parameters:
json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getBodySb
Copies the response body to a Chilkat StringBuilder object.- Parameters:
sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getBodyXml
Loads the XML response body into an XML object. This method should only be called when it is known that the response body contains XML.- Parameters:
xml- thexmlargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCookieDomain
Returns the domain of the Nth cookie in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCookieExpiresStr
Returns the expiration date/time of the Nth cookie in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCookieName
Returns the name of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCookiePath
Returns the path of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCookieValue
Returns the value of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getHeaderField
Returns the value of a response header field accessed by field name.- Parameters:
fieldName- thefieldNameargument (notnull)- Returns:
- the result, or
nullif not found (no exception is thrown in that case)
-
getHeaderFieldAttr
Returns a response header field attribute. As an example, the response charset is simply the GetHeaderFieldAttr(content-type,charset)- Parameters:
fieldName- thefieldNameargument (notnull)attrName- theattrNameargument (notnull)- Returns:
- the result, or
nullif not found (no exception is thrown in that case)
-
getHeaderName
Gets the name of the Nth response header field. Indexing begins at 0. The number of response headers is specified by the NumHeaderFields property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getHeaderValue
Gets the value of the Nth response header field. Indexing begins at 0. The number of response headers is specified by the NumHeaderFields property.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveBodyBinary
Saves the body of the HTTP response to a file.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveBodyText
Saves the HTTP response body to a file. This method provides control over CRLF vs bare-LF line-endings. IfbCrlfistrue, then line endings are automatically converted to CRLF if necessary. IfbCrlfisfalse, then line-endings are automatically converted to bare-LF's (Unix style) if necessary.To save the HTTP response body exactly as-is (with no line-ending manipulation), then call SaveBodyBinary.
- Parameters:
bCrlf- thebCrlfargumentpath- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
urlEncParamValue
Convenience method for parsing a param's value out of a URL-encoded param string. For example, if a caller passes the following string inencodedParamString:oauth_token=ABC&oauth_token_secret=123&oauth_callback_confirmed=true
and
oauth_token_secretinparamName, then the return value would be123.- Parameters:
encodedParamString- theencodedParamStringargument (notnull)paramName- theparamNameargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-