- Java net httpurlconnection example
- Field Summary
- Fields inherited from class java.net.URLConnection
- Constructor Summary
- Method Summary
- Methods inherited from class java.net.URLConnection
- Methods inherited from class java.lang.Object
- Field Detail
- method
- chunkLength
- fixedContentLength
- fixedContentLengthLong
- responseCode
- responseMessage
- instanceFollowRedirects
- HTTP_OK
- HTTP_CREATED
- HTTP_ACCEPTED
- HTTP_NOT_AUTHORITATIVE
- HTTP_NO_CONTENT
- HTTP_RESET
- HTTP_PARTIAL
- HTTP_MULT_CHOICE
- HTTP_MOVED_PERM
- HTTP_MOVED_TEMP
- HTTP_SEE_OTHER
- HTTP_NOT_MODIFIED
- HTTP_USE_PROXY
- HTTP_BAD_REQUEST
- HTTP_UNAUTHORIZED
- HTTP_PAYMENT_REQUIRED
- HTTP_FORBIDDEN
- HTTP_NOT_FOUND
- HTTP_BAD_METHOD
- HTTP_NOT_ACCEPTABLE
- HTTP_PROXY_AUTH
- HTTP_CLIENT_TIMEOUT
- HTTP_CONFLICT
- HTTP_GONE
- HTTP_LENGTH_REQUIRED
- HTTP_PRECON_FAILED
- HTTP_ENTITY_TOO_LARGE
- HTTP_REQ_TOO_LONG
- HTTP_UNSUPPORTED_TYPE
- HTTP_SERVER_ERROR
- HTTP_INTERNAL_ERROR
- HTTP_NOT_IMPLEMENTED
- HTTP_BAD_GATEWAY
- HTTP_UNAVAILABLE
- HTTP_GATEWAY_TIMEOUT
- HTTP_VERSION
- Constructor Detail
- HttpURLConnection
- Method Detail
- getHeaderFieldKey
- setFixedLengthStreamingMode
- setFixedLengthStreamingMode
- setChunkedStreamingMode
- getHeaderField
- setFollowRedirects
- getFollowRedirects
- setInstanceFollowRedirects
- getInstanceFollowRedirects
- setRequestMethod
- getRequestMethod
- getResponseCode
- getResponseMessage
- getHeaderFieldDate
- disconnect
- usingProxy
- getPermission
- getErrorStream
Java net httpurlconnection example
If automatic redirection is enabled, and this request is redirected to another destination, then the caller must also have permission to connect to the redirected host/URL.
Field Summary
Fields inherited from class java.net.URLConnection
Constructor Summary
Method Summary
Returns a SocketPermission object representing the permission necessary to connect to the destination host and port.
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance.
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance.
Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.
Methods inherited from class java.net.URLConnection
Methods inherited from class java.lang.Object
Field Detail
method
chunkLength
protected int chunkLength
The chunk-length when using chunked encoding streaming mode for output. A value of -1 means chunked encoding is disabled for output.
fixedContentLength
protected int fixedContentLength
The fixed content-length when using fixed-length streaming mode. A value of -1 means fixed-length streaming mode is disabled for output. NOTE: fixedContentLengthLong is recommended instead of this field, as it allows larger content lengths to be set.
fixedContentLengthLong
protected long fixedContentLengthLong
The fixed content-length when using fixed-length streaming mode. A value of -1 means fixed-length streaming mode is disabled for output.
responseCode
protected int responseCode
- 1xx: Informational
- 2xx: Success
- 3xx: Redirection
- 4xx: Client Error
- 5xx: Server Error
responseMessage
instanceFollowRedirects
protected boolean instanceFollowRedirects
If true , the protocol will automatically follow redirects. If false , the protocol will not automatically follow redirects. This field is set by the setInstanceFollowRedirects method. Its value is returned by the getInstanceFollowRedirects method. Its default value is based on the value of the static followRedirects at HttpURLConnection construction time.
HTTP_OK
public static final int HTTP_OK
HTTP_CREATED
public static final int HTTP_CREATED
HTTP_ACCEPTED
public static final int HTTP_ACCEPTED
HTTP_NOT_AUTHORITATIVE
public static final int HTTP_NOT_AUTHORITATIVE
HTTP_NO_CONTENT
public static final int HTTP_NO_CONTENT
HTTP_RESET
public static final int HTTP_RESET
HTTP_PARTIAL
public static final int HTTP_PARTIAL
HTTP_MULT_CHOICE
public static final int HTTP_MULT_CHOICE
HTTP_MOVED_PERM
public static final int HTTP_MOVED_PERM
HTTP_MOVED_TEMP
public static final int HTTP_MOVED_TEMP
HTTP_SEE_OTHER
public static final int HTTP_SEE_OTHER
HTTP_NOT_MODIFIED
public static final int HTTP_NOT_MODIFIED
HTTP_USE_PROXY
public static final int HTTP_USE_PROXY
HTTP_BAD_REQUEST
public static final int HTTP_BAD_REQUEST
HTTP_UNAUTHORIZED
public static final int HTTP_UNAUTHORIZED
HTTP_PAYMENT_REQUIRED
public static final int HTTP_PAYMENT_REQUIRED
HTTP_FORBIDDEN
public static final int HTTP_FORBIDDEN
HTTP_NOT_FOUND
public static final int HTTP_NOT_FOUND
HTTP_BAD_METHOD
public static final int HTTP_BAD_METHOD
HTTP_NOT_ACCEPTABLE
public static final int HTTP_NOT_ACCEPTABLE
HTTP_PROXY_AUTH
public static final int HTTP_PROXY_AUTH
HTTP_CLIENT_TIMEOUT
public static final int HTTP_CLIENT_TIMEOUT
HTTP_CONFLICT
public static final int HTTP_CONFLICT
HTTP_GONE
public static final int HTTP_GONE
HTTP_LENGTH_REQUIRED
public static final int HTTP_LENGTH_REQUIRED
HTTP_PRECON_FAILED
public static final int HTTP_PRECON_FAILED
HTTP_ENTITY_TOO_LARGE
public static final int HTTP_ENTITY_TOO_LARGE
HTTP_REQ_TOO_LONG
public static final int HTTP_REQ_TOO_LONG
HTTP_UNSUPPORTED_TYPE
public static final int HTTP_UNSUPPORTED_TYPE
HTTP_SERVER_ERROR
@Deprecated public static final int HTTP_SERVER_ERROR
HTTP_INTERNAL_ERROR
public static final int HTTP_INTERNAL_ERROR
HTTP_NOT_IMPLEMENTED
public static final int HTTP_NOT_IMPLEMENTED
HTTP_BAD_GATEWAY
public static final int HTTP_BAD_GATEWAY
HTTP_UNAVAILABLE
public static final int HTTP_UNAVAILABLE
HTTP_GATEWAY_TIMEOUT
public static final int HTTP_GATEWAY_TIMEOUT
HTTP_VERSION
public static final int HTTP_VERSION
Constructor Detail
HttpURLConnection
Method Detail
getHeaderFieldKey
Returns the key for the n th header field. Some implementations may treat the 0 th header field as special, i.e. as the status line returned by the HTTP server. In this case, getHeaderField(0) returns the status line, but getHeaderFieldKey(0) returns null.
setFixedLengthStreamingMode
public void setFixedLengthStreamingMode(int contentLength)
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance. An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount. When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error. This method must be called before the URLConnection is connected. NOTE: setFixedLengthStreamingMode(long) is recommended instead of this method as it allows larger content lengths to be set.
setFixedLengthStreamingMode
public void setFixedLengthStreamingMode(long contentLength)
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance. An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount. When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error. This method must be called before the URLConnection is connected. The content length set by invoking this method takes precedence over any value set by setFixedLengthStreamingMode(int) .
setChunkedStreamingMode
public void setChunkedStreamingMode(int chunklen)
This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance. In this mode, chunked transfer encoding is used to send the request body. Note, not all HTTP servers support this mode. When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error. This method must be called before the URLConnection is connected.
getHeaderField
Returns the value for the n th header field. Some implementations may treat the 0 th header field as special, i.e. as the status line returned by the HTTP server. This method can be used in conjunction with the getHeaderFieldKey method to iterate through all the headers in the message.
setFollowRedirects
public static void setFollowRedirects(boolean set)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class. True by default. Applets cannot change this variable. If there is a security manager, this method first calls the security manager’s checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.
getFollowRedirects
public static boolean getFollowRedirects()
setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance. The default value comes from followRedirects, which defaults to true.
getInstanceFollowRedirects
public boolean getInstanceFollowRedirects()
setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
getRequestMethod
getResponseCode
Gets the status code from an HTTP response message. For example, in the case of the following status lines:
HTTP/1.0 200 OK HTTP/1.0 401 Unauthorized
It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).
getResponseMessage
public String getResponseMessage() throws IOException
Gets the HTTP response message, if any, returned along with the response code from a server. From responses like:
HTTP/1.0 200 OK HTTP/1.0 404 Not Found
Extracts the Strings «OK» and «Not Found» respectively. Returns null if none could be discerned from the responses (the result was not valid HTTP).
getHeaderFieldDate
Returns the value of the named field parsed as date. The result is the number of milliseconds since January 1, 1970 GMT represented by the named field. This form of getHeaderField exists because some connection types (e.g., http-ng ) have pre-parsed headers. Classes for that connection type can override this method and short-circuit the parsing.
disconnect
public abstract void disconnect()
Indicates that other requests to the server are unlikely in the near future. Calling disconnect() should not imply that this HttpURLConnection instance can be reused for other requests.
usingProxy
public abstract boolean usingProxy()
getPermission
public Permission getPermission() throws IOException
Returns a SocketPermission object representing the permission necessary to connect to the destination host and port.
getErrorStream
Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do. This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.