Class HttpURLConnection
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe 202 status.static final intThe 502 status.static final intThe 400 status.static final intThe 409 status.static final intThe 201 status.static final intThe 403 status.static final intThe 500 status.static final intThe 301 status.static final intThe 302 status.static final intThe 204 status.static final intThe 404 status.static final intThe 304 status.static final intThe 200 status.static final intThe 429 status, which the platform's own rate limits also use.static final intThe 401 status.static final intThe 503 status.Fields inherited from class dev.gmitch215.bytebox.net.URLConnection
connected, connectTimeout, doInput, doOutput, readTimeout, url -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRequestProperty(String name, String value) Adds a request header, keeping any value already set.voidconnect()Sends the request, if it has not been sent.voidReleases the response.The body of a failing response, whichgetInputStream()throws rather than return.getHeaderField(String name) One response header.Every response header.The body.booleanReturns whether a redirect will be followed.Where to write the request body.Returns the method that will be used.getRequestProperty(String name) One request header, as set.intThe status.The status text.voidsetInstanceFollowRedirects(boolean followRedirects) Whether to follow a redirect rather than return it.voidsetRequestMethod(String method) The method to use.voidsetRequestProperty(String name, String value) Sets a request header, replacing any value already set.booleanReturns whether a proxy is in use, which is never: the platform has no proxy setting.Methods inherited from class dev.gmitch215.bytebox.net.URLConnection
getConnectTimeout, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDoInput, getDoOutput, getHeaderFieldInt, getHeaderFieldLong, getReadTimeout, getURL, getUseCaches, setConnectTimeout, setDoInput, setDoOutput, setReadTimeout, setUseCaches
-
Field Details
-
HTTP_OK
public static final int HTTP_OKThe 200 status.- See Also:
-
HTTP_CREATED
public static final int HTTP_CREATEDThe 201 status.- See Also:
-
HTTP_ACCEPTED
public static final int HTTP_ACCEPTEDThe 202 status.- See Also:
-
HTTP_NO_CONTENT
public static final int HTTP_NO_CONTENTThe 204 status.- See Also:
-
HTTP_MOVED_PERM
public static final int HTTP_MOVED_PERMThe 301 status.- See Also:
-
HTTP_MOVED_TEMP
public static final int HTTP_MOVED_TEMPThe 302 status.- See Also:
-
HTTP_NOT_MODIFIED
public static final int HTTP_NOT_MODIFIEDThe 304 status.- See Also:
-
HTTP_BAD_REQUEST
public static final int HTTP_BAD_REQUESTThe 400 status.- See Also:
-
HTTP_UNAUTHORIZED
public static final int HTTP_UNAUTHORIZEDThe 401 status.- See Also:
-
HTTP_FORBIDDEN
public static final int HTTP_FORBIDDENThe 403 status.- See Also:
-
HTTP_NOT_FOUND
public static final int HTTP_NOT_FOUNDThe 404 status.- See Also:
-
HTTP_CONFLICT
public static final int HTTP_CONFLICTThe 409 status.- See Also:
-
HTTP_TOO_MANY_REQUESTS
public static final int HTTP_TOO_MANY_REQUESTSThe 429 status, which the platform's own rate limits also use.- See Also:
-
HTTP_INTERNAL_ERROR
public static final int HTTP_INTERNAL_ERRORThe 500 status.- See Also:
-
HTTP_BAD_GATEWAY
public static final int HTTP_BAD_GATEWAYThe 502 status.- See Also:
-
HTTP_UNAVAILABLE
public static final int HTTP_UNAVAILABLEThe 503 status.- See Also:
-
-
Constructor Details
-
HttpURLConnection
- Parameters:
url- the URL to request
-
-
Method Details
-
setRequestMethod
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
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
The status.- Returns:
- the status code
- Throws:
IOException- when the request fails
-
getResponseMessage
The status text.- Returns:
- the reason phrase, which the platform leaves empty for most statuses
- Throws:
IOException- when the request fails
-
connect
Description copied from class:URLConnectionSends the request, if it has not been sent.- Specified by:
connectin classURLConnection- Throws:
IOException- when the request fails
-
getInputStream
Description copied from class:URLConnectionThe body.- Specified by:
getInputStreamin classURLConnection- Returns:
- the body, which is read in full before the stream is returned
- Throws:
IOException- when the request fails
-
getErrorStream
The body of a failing response, whichgetInputStream()throws rather than return.- Returns:
- the body, or null when the request has not been sent or did not fail
-
getOutputStream
Description copied from class:URLConnectionWhere to write the request body.- Specified by:
getOutputStreamin classURLConnection- 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
Description copied from class:URLConnectionOne response header.- Specified by:
getHeaderFieldin classURLConnection- Parameters:
name- the header name, matched without regard to case- Returns:
- the value, or null when the response does not carry it
-
getHeaderFields
Every response header.- Returns:
- the headers, keyed in lower case because HTTP does not distinguish
-
setRequestProperty
Description copied from class:URLConnectionSets a request header, replacing any value already set.- Specified by:
setRequestPropertyin classURLConnection- Parameters:
name- the header namevalue- the value
-
addRequestProperty
Description copied from class:URLConnectionAdds a request header, keeping any value already set.- Specified by:
addRequestPropertyin classURLConnection- Parameters:
name- the header namevalue- the value
-
getRequestProperty
Description copied from class:URLConnectionOne request header, as set.- Specified by:
getRequestPropertyin classURLConnection- 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
-