Class HttpURLConnection

java.lang.Object
dev.gmitch215.bytebox.net.URLConnection
dev.gmitch215.bytebox.net.HttpURLConnection

public class HttpURLConnection extends URLConnection
An HTTP request, standing in for java.net.HttpURLConnection.

The compiler substitutes java.net.HttpURLConnection for this one, so a library that makes a request the ordinary way works unchanged. Underneath it is fetch, which counts against the subrequest limit - 50 per invocation on the free plan, 10,000 on paid - and against the six simultaneous outgoing connections every plan allows.

One difference worth knowing, and it follows from fetch being one call rather than a duplex stream: the request is not sent until the response is asked for. A body written to getOutputStream() is collected, and getResponseCode(), getInputStream() or a header lookup is what sends it. The response body is read in full before getInputStream() returns, so the stream never blocks.

The two timeouts are added together, because the platform gives one deadline for a whole request rather than a separate one for the connection.

Since:
1.0.0
  • Field Details

    • HTTP_OK

      public static final int HTTP_OK
      The 200 status.
      See Also:
    • HTTP_CREATED

      public static final int HTTP_CREATED
      The 201 status.
      See Also:
    • HTTP_ACCEPTED

      public static final int HTTP_ACCEPTED
      The 202 status.
      See Also:
    • HTTP_NO_CONTENT

      public static final int HTTP_NO_CONTENT
      The 204 status.
      See Also:
    • HTTP_MOVED_PERM

      public static final int HTTP_MOVED_PERM
      The 301 status.
      See Also:
    • HTTP_MOVED_TEMP

      public static final int HTTP_MOVED_TEMP
      The 302 status.
      See Also:
    • HTTP_NOT_MODIFIED

      public static final int HTTP_NOT_MODIFIED
      The 304 status.
      See Also:
    • HTTP_BAD_REQUEST

      public static final int HTTP_BAD_REQUEST
      The 400 status.
      See Also:
    • HTTP_UNAUTHORIZED

      public static final int HTTP_UNAUTHORIZED
      The 401 status.
      See Also:
    • HTTP_FORBIDDEN

      public static final int HTTP_FORBIDDEN
      The 403 status.
      See Also:
    • HTTP_NOT_FOUND

      public static final int HTTP_NOT_FOUND
      The 404 status.
      See Also:
    • HTTP_CONFLICT

      public static final int HTTP_CONFLICT
      The 409 status.
      See Also:
    • HTTP_TOO_MANY_REQUESTS

      public static final int HTTP_TOO_MANY_REQUESTS
      The 429 status, which the platform's own rate limits also use.
      See Also:
    • HTTP_INTERNAL_ERROR

      public static final int HTTP_INTERNAL_ERROR
      The 500 status.
      See Also:
    • HTTP_BAD_GATEWAY

      public static final int HTTP_BAD_GATEWAY
      The 502 status.
      See Also:
    • HTTP_UNAVAILABLE

      public static final int HTTP_UNAVAILABLE
      The 503 status.
      See Also:
  • Constructor Details

    • HttpURLConnection

      protected HttpURLConnection(URL url)
      Parameters:
      url - the URL to request
  • Method Details

    • setRequestMethod

      public void setRequestMethod(String method) throws ProtocolException
      The method to use.
      Parameters:
      method - one of GET, POST, HEAD, OPTIONS, PUT, DELETE, PATCH or TRACE
      Throws:
      ProtocolException - when it is not one of those, or the request is already sent
    • getRequestMethod

      public String getRequestMethod()
      Returns the method that will be used.
      Returns:
      the method that will be used
    • setInstanceFollowRedirects

      public void setInstanceFollowRedirects(boolean followRedirects)
      Whether to follow a redirect rather than return it.
      Parameters:
      followRedirects - whether to
    • getInstanceFollowRedirects

      public boolean getInstanceFollowRedirects()
      Returns whether a redirect will be followed.
      Returns:
      whether a redirect will be followed
    • getResponseCode

      public int getResponseCode() throws IOException
      The status.
      Returns:
      the status code
      Throws:
      IOException - when the request fails
    • getResponseMessage

      public String getResponseMessage() throws IOException
      The status text.
      Returns:
      the reason phrase, which the platform leaves empty for most statuses
      Throws:
      IOException - when the request fails
    • connect

      public void connect() throws IOException
      Description copied from class: URLConnection
      Sends the request, if it has not been sent.
      Specified by:
      connect in class URLConnection
      Throws:
      IOException - when the request fails
    • getInputStream

      public InputStream getInputStream() throws IOException
      Description copied from class: URLConnection
      The body.
      Specified by:
      getInputStream in class URLConnection
      Returns:
      the body, which is read in full before the stream is returned
      Throws:
      IOException - when the request fails
    • getErrorStream

      public InputStream getErrorStream()
      The body of a failing response, which getInputStream() throws rather than return.
      Returns:
      the body, or null when the request has not been sent or did not fail
    • getOutputStream

      public OutputStream getOutputStream() throws IOException
      Description copied from class: URLConnection
      Where to write the request body.
      Specified by:
      getOutputStream in class URLConnection
      Returns:
      the stream, which is collected and sent when the response is asked for
      Throws:
      IOException - when the connection does not accept a body
    • getHeaderField

      public String getHeaderField(String name)
      Description copied from class: URLConnection
      One response header.
      Specified by:
      getHeaderField in class URLConnection
      Parameters:
      name - the header name, matched without regard to case
      Returns:
      the value, or null when the response does not carry it
    • getHeaderFields

      public Map<String,List<String>> getHeaderFields()
      Every response header.
      Returns:
      the headers, keyed in lower case because HTTP does not distinguish
    • setRequestProperty

      public void setRequestProperty(String name, String value)
      Description copied from class: URLConnection
      Sets a request header, replacing any value already set.
      Specified by:
      setRequestProperty in class URLConnection
      Parameters:
      name - the header name
      value - the value
    • addRequestProperty

      public void addRequestProperty(String name, String value)
      Description copied from class: URLConnection
      Adds a request header, keeping any value already set.
      Specified by:
      addRequestProperty in class URLConnection
      Parameters:
      name - the header name
      value - the value
    • getRequestProperty

      public String getRequestProperty(String name)
      Description copied from class: URLConnection
      One request header, as set.
      Specified by:
      getRequestProperty in class URLConnection
      Parameters:
      name - the header name
      Returns:
      the value, or null when none was set
    • disconnect

      public void disconnect()
      Releases the response.

      The platform closes a connection when the invocation ends rather than when a caller says so, so this drops the response this object holds and nothing more.

    • usingProxy

      public boolean usingProxy()
      Returns whether a proxy is in use, which is never: the platform has no proxy setting.
      Returns:
      whether a proxy is in use, which is never: the platform has no proxy setting