Class HttpResponse

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.HttpResponse
All Implemented Interfaces:
AutoCloseable

public class HttpResponse extends ChilkatObject
<section class="httpresponse-summary">

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 Details

    • HttpResponse

      public HttpResponse()
      Creates a new HttpResponse object.
  • Method Details

    • getBodyQP

      public String getBodyQP()
      The same as the Body property, but returned as a quoted-printable encoded string.
      Returns:
      the value of the BodyQP property (read-only)
    • getBodyStr

      public String getBodyStr()
      The response body returned as a string.
      Returns:
      the value of the BodyStr property (read-only)
    • getCharset

      public String getCharset()
      The response charset, such as iso-8859-1, if applicable. Obviously, responses for GIF and JPG files will not have a charset.
      Returns:
      the value of the Charset property (read-only)
    • getContentLength

      public long getContentLength()
      The content length of the response, in bytes.
      Returns:
      the value of the ContentLength property (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 ContentLength64 property (read-only)
    • getDateStr

      public String getDateStr()
      Returns the content of the Date response header field in RFC822 date/time string format.
      Returns:
      the value of the DateStr property (read-only)
    • getDomain

      public String getDomain()
      The domain of the HTTP server that created this response.
      Returns:
      the value of the Domain property (read-only)
    • getFinalRedirectUrl

      public String getFinalRedirectUrl()
      Returns the redirect URL for 301/302 responses.
      Returns:
      the value of the FinalRedirectUrl property (read-only)
    • getFullMime

      public String getFullMime()
      Returns the full MIME (header + body) of the HTTP response.
      Returns:
      the value of the FullMime property (read-only)
    • getHeader

      public String getHeader()
      The full text of the response header.
      Returns:
      the value of the Header property (read-only)
    • getNumCookies

      public int getNumCookies()
      The number of cookies included in the response.
      Returns:
      the value of the NumCookies property (read-only)
    • getNumHeaderFields

      public int getNumHeaderFields()
      The number of response header fields.
      Returns:
      the value of the NumHeaderFields property (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 StatusCode property (read-only)
    • getStatusLine

      public String getStatusLine()
      The first line of an HTTP response, such as HTTP/1.0 200 OK.
      Returns:
      the value of the StatusLine property (read-only)
    • getStatusText

      public String 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 is HTTP/1.0 200 OK, then this property contains OK.
      Returns:
      the value of the StatusText property (read-only)
    • getUncommonOptions

      public String 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 UncommonOptions property
    • setUncommonOptions

      public void setUncommonOptions​(String value)
      Sets the UncommonOptions property; see getUncommonOptions().
      Parameters:
      value - the new value (not null)
    • getBodyBd

      public void getBodyBd​(BinData binData)
      Copies the response body to a BinData object.
      Parameters:
      binData - the binData argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getBodyJarr

      public void getBodyJarr​(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 ']')
      Parameters:
      jarr - the jarr argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getBodyJson

      public void getBodyJson​(JsonObject json)
      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 - the json argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getBodySb

      public void getBodySb​(StringBuilder sb)
      Copies the response body to a Chilkat StringBuilder object.
      Parameters:
      sb - the sb argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getBodyXml

      public void getBodyXml​(Xml xml)
      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 - the xml argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getCookieDomain

      public String getCookieDomain​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getCookieExpiresStr

      public String getCookieExpiresStr​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getCookieName

      public String getCookieName​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getCookiePath

      public String getCookiePath​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getCookieValue

      public String getCookieValue​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getHeaderField

      public String getHeaderField​(String fieldName)
      Returns the value of a response header field accessed by field name.
      Parameters:
      fieldName - the fieldName argument (not null)
      Returns:
      the result, or null if not found (no exception is thrown in that case)
    • getHeaderFieldAttr

      public String getHeaderFieldAttr​(String fieldName, String attrName)
      Returns a response header field attribute. As an example, the response charset is simply the GetHeaderFieldAttr(content-type,charset)
      Parameters:
      fieldName - the fieldName argument (not null)
      attrName - the attrName argument (not null)
      Returns:
      the result, or null if not found (no exception is thrown in that case)
    • getHeaderName

      public String getHeaderName​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getHeaderValue

      public String getHeaderValue​(int index)
      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 - the index argument
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveBodyBinary

      public void saveBodyBinary​(String path)
      Saves the body of the HTTP response to a file.
      Parameters:
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveBodyText

      public void saveBodyText​(boolean bCrlf, String path)
      Saves the HTTP response body to a file. This method provides control over CRLF vs bare-LF line-endings. If bCrlf is true, then line endings are automatically converted to CRLF if necessary. If bCrlf is false, 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 - the bCrlf argument
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • urlEncParamValue

      public String urlEncParamValue​(String encodedParamString, String paramName)
      Convenience method for parsing a param's value out of a URL-encoded param string. For example, if a caller passes the following string in encodedParamString:
      oauth_token=ABC&oauth_token_secret=123&oauth_callback_confirmed=true

      and oauth_token_secret in paramName, then the return value would be 123.

      Parameters:
      encodedParamString - the encodedParamString argument (not null)
      paramName - the paramName argument (not null)
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why