3909N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A URLConnection with support for HTTP-specific features. See 0N/A * Each HttpURLConnection instance is used to make a single request 0N/A * but the underlying network connection to the HTTP server may be 0N/A * transparently shared by other instances. Calling the close() methods 0N/A * on the InputStream or OutputStream of an HttpURLConnection 0N/A * after a request may free network resources associated with this 0N/A * instance but has no effect on any shared persistent connection. 0N/A * Calling the disconnect() method may close the underlying socket 0N/A * if a persistent connection is otherwise idle at that time. 0N/A * <P>The HTTP protocol handler has a few settings that can be accessed through 0N/A * System Properties. This covers 0N/A * @see java.net.HttpURLConnection#disconnect() 0N/A /* instance variables */ 0N/A * The HTTP method (GET,POST,PUT,etc.). 0N/A * The chunk-length when using chunked encoding streaming mode for output. 0N/A * A value of <code>-1</code> means chunked encoding is disabled for output. 0N/A * The fixed content-length when using fixed-length streaming mode. 0N/A * A value of <code>-1</code> means fixed-length streaming mode is disabled 705N/A * <P> <B>NOTE:</B> {@link #fixedContentLengthLong} is recommended instead 705N/A * of this field, as it allows larger content lengths to be set. 705N/A * The fixed content-length when using fixed-length streaming mode. 705N/A * A value of {@code -1} means fixed-length streaming mode is disabled 0N/A * Returns the key for the <code>n</code><sup>th</sup> header field. 0N/A * Some implementations may treat the <code>0</code><sup>th</sup> 0N/A * header field as special, i.e. as the status line returned by the HTTP 0N/A * server. In this case, {@link #getHeaderField(int) getHeaderField(0)} returns the status 0N/A * line, but <code>getHeaderFieldKey(0)</code> returns null. 0N/A * @param n an index, where n >=0. 0N/A * @return the key for the <code>n</code><sup>th</sup> header field, 0N/A * or <code>null</code> if the key does not exist. 0N/A * This method is used to enable streaming of a HTTP request body 0N/A * without internal buffering, when the content length is known in 0N/A * An exception will be thrown if the application 0N/A * attempts to write more data than the indicated 0N/A * content-length, or if the application closes the OutputStream 0N/A * before writing the indicated amount. 0N/A * When output streaming is enabled, authentication 0N/A * and redirection cannot be handled automatically. 0N/A * A HttpRetryException will be thrown when reading 0N/A * the response if authentication or redirection are required. 0N/A * This exception can be queried for the details of the error. 0N/A * This method must be called before the URLConnection is connected. 705N/A * <B>NOTE:</B> {@link #setFixedLengthStreamingMode(long)} is recommended 705N/A * instead of this method as it allows larger content lengths to be set. 0N/A * @param contentLength The number of bytes which will be written 0N/A * to the OutputStream. 0N/A * @throws IllegalStateException if URLConnection is already connected 0N/A * or if a different streaming mode is already enabled. 0N/A * @throws IllegalArgumentException if a content length less than 0N/A * zero is specified. 0N/A * @see #setChunkedStreamingMode(int) 705N/A * This method is used to enable streaming of a HTTP request body 705N/A * without internal buffering, when the content length is known in 705N/A * <P> An exception will be thrown if the application attempts to write 705N/A * more data than the indicated content-length, or if the application 705N/A * closes the OutputStream before writing the indicated amount. 705N/A * <P> When output streaming is enabled, authentication and redirection 705N/A * cannot be handled automatically. A {@linkplain HttpRetryException} will 705N/A * be thrown when reading the response if authentication or redirection 705N/A * are required. This exception can be queried for the details of the 705N/A * <P> This method must be called before the URLConnection is connected. 705N/A * <P> The content length set by invoking this method takes precedence 705N/A * over any value set by {@link #setFixedLengthStreamingMode(int)}. 705N/A * The number of bytes which will be written to the OutputStream. 705N/A * @throws IllegalStateException 705N/A * if URLConnection is already connected or if a different 705N/A * streaming mode is already enabled. 705N/A * @throws IllegalArgumentException 705N/A * if a content length less than zero is specified. 705N/A "Chunked encoding streaming mode set");
0N/A /* Default chunk size (including chunk header) if not specified; 0N/A * we want to keep this in sync with the one defined in 0N/A * sun.net.www.http.ChunkedOutputStream 0N/A * This method is used to enable streaming of a HTTP request body 0N/A * without internal buffering, when the content length is <b>not</b> 0N/A * known in advance. In this mode, chunked transfer encoding 0N/A * is used to send the request body. Note, not all HTTP servers 0N/A * support this mode. 0N/A * When output streaming is enabled, authentication 0N/A * and redirection cannot be handled automatically. 0N/A * A HttpRetryException will be thrown when reading 0N/A * the response if authentication or redirection are required. 0N/A * This exception can be queried for the details of the error. 0N/A * This method must be called before the URLConnection is connected. 0N/A * @param chunklen The number of bytes to write in each chunk. 0N/A * If chunklen is less than or equal to zero, a default 0N/A * value will be used. 0N/A * @throws IllegalStateException if URLConnection is already connected 0N/A * or if a different streaming mode is already enabled. 0N/A * @see #setFixedLengthStreamingMode(int) 0N/A * Returns the value for the <code>n</code><sup>th</sup> header field. 0N/A * Some implementations may treat the <code>0</code><sup>th</sup> 0N/A * header field as special, i.e. as the status line returned by the HTTP 0N/A * This method can be used in conjunction with the 0N/A * {@link #getHeaderFieldKey getHeaderFieldKey} method to iterate through all 0N/A * the headers in the message. 0N/A * @param n an index, where n>=0. 0N/A * @return the value of the <code>n</code><sup>th</sup> header field, 0N/A * or <code>null</code> if the value does not exist. 0N/A * @see java.net.HttpURLConnection#getHeaderFieldKey(int) 0N/A * An <code>int</code> representing the three digit HTTP Status-Code. 0N/A * <li> 1xx: Informational 0N/A * <li> 3xx: Redirection 0N/A * <li> 4xx: Client Error 0N/A * <li> 5xx: Server Error 0N/A * The HTTP response message. 0N/A /* static variables */ 0N/A /* do we automatically follow redirects? The default is true. */ 0N/A * If <code>true</code>, the protocol will automatically follow redirects. 0N/A * If <code>false</code>, the protocol will not automatically follow 0N/A * This field is set by the <code>setInstanceFollowRedirects</code> 0N/A * method. Its value is returned by the <code>getInstanceFollowRedirects</code> 0N/A * Its default value is based on the value of the static followRedirects 0N/A * at HttpURLConnection construction time. 0N/A * @see java.net.HttpURLConnection#setInstanceFollowRedirects(boolean) 0N/A * @see java.net.HttpURLConnection#getInstanceFollowRedirects() 0N/A * @see java.net.HttpURLConnection#setFollowRedirects(boolean) 0N/A /* valid HTTP methods */ 0N/A "GET",
"POST",
"HEAD",
"OPTIONS",
"PUT",
"DELETE",
"TRACE" 0N/A * Constructor for the HttpURLConnection. 0N/A * Sets whether HTTP redirects (requests with response code 3xx) should 0N/A * be automatically followed by this class. True by default. Applets 0N/A * cannot change this variable. 0N/A * If there is a security manager, this method first calls 0N/A * the security manager's <code>checkSetFactory</code> method 0N/A * to ensure the operation is allowed. 0N/A * This could result in a SecurityException. 0N/A * @param set a <code>boolean</code> indicating whether or not 0N/A * to follow HTTP redirects. 0N/A * @exception SecurityException if a security manager exists and its 0N/A * <code>checkSetFactory</code> method doesn't 0N/A * allow the operation. 0N/A * @see SecurityManager#checkSetFactory 0N/A * @see #getFollowRedirects() 0N/A // seems to be the best check here... 0N/A * Returns a <code>boolean</code> indicating 0N/A * whether or not HTTP redirects (3xx) should 0N/A * be automatically followed. 0N/A * @return <code>true</code> if HTTP redirects should 0N/A * be automatically followed, <tt>false</tt> if not. 0N/A * @see #setFollowRedirects(boolean) 0N/A * Sets whether HTTP redirects (requests with response code 3xx) should 0N/A * be automatically followed by this <code>HttpURLConnection</code> 0N/A * The default value comes from followRedirects, which defaults to 0N/A * @param followRedirects a <code>boolean</code> indicating 0N/A * whether or not to follow HTTP redirects. 0N/A * @see java.net.HttpURLConnection#instanceFollowRedirects 0N/A * @see #getInstanceFollowRedirects 0N/A * Returns the value of this <code>HttpURLConnection</code>'s 0N/A * <code>instanceFollowRedirects</code> field. 0N/A * @return the value of this <code>HttpURLConnection</code>'s 0N/A * <code>instanceFollowRedirects</code> field. 0N/A * @see java.net.HttpURLConnection#instanceFollowRedirects 0N/A * @see #setInstanceFollowRedirects(boolean) 0N/A * Set the method for the URL request, one of: 0N/A * </UL> are legal, subject to protocol restrictions. The default 0N/A * @param method the HTTP method 0N/A * @exception ProtocolException if the method cannot be reset or if 0N/A * the requested method isn't valid for HTTP. 2909N/A * @exception SecurityException if a security manager is set and the 3669N/A * method is "TRACE", but the "allowHttpTrace" 3669N/A * NetPermission is not granted. 0N/A * @see #getRequestMethod() 0N/A // This restriction will prevent people from using this class to 0N/A // experiment w/ new HTTP methods using java. But it should 0N/A // be placed for security - the request String could be 0N/A // arbitrarily long. 0N/A * Get the request method. 0N/A * @return the HTTP request method 0N/A * @see #setRequestMethod(java.lang.String) 0N/A * Gets the status code from an HTTP response message. 0N/A * For example, in the case of the following status lines: 0N/A * HTTP/1.0 401 Unauthorized 0N/A * It will return 200 and 401 respectively. 0N/A * Returns -1 if no code can be discerned 0N/A * from the response (i.e., the response is not valid HTTP). 0N/A * @throws IOException if an error occurred connecting to the server. 0N/A * @return the HTTP Status-Code, or -1 0N/A * We're got the response code already 0N/A * Ensure that we have connected to the server. Record 0N/A * exception as we need to re-throw it if there isn't 0N/A * If we can't a status-line then re-throw any exception 0N/A * that getInputStream threw. 0N/A * Examine the status-line - should be formatted as per 0N/A * section 6.1 of RFC 2616 :- 0N/A * Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase 0N/A * If status line can't be parsed return -1. 0N/A // deviation from RFC 2616 - don't reject status line 0N/A // if SP Reason-Phrase is not included. 0N/A * Gets the HTTP response message, if any, returned along with the 0N/A * response code from a server. From responses like: 0N/A * HTTP/1.0 404 Not Found 0N/A * Extracts the Strings "OK" and "Not Found" respectively. 0N/A * Returns null if none could be discerned from the responses 0N/A * (the result was not valid HTTP). 0N/A * @throws IOException if an error occurred connecting to the server. 0N/A * @return the HTTP response message, or <code>null</code> 0N/A * Indicates that other requests to the server 0N/A * are unlikely in the near future. Calling disconnect() 0N/A * should not imply that this HttpURLConnection 0N/A * instance can be reused for other requests. 0N/A * Indicates if the connection is going through a proxy. 0N/A * @return a boolean indicating if the connection is 0N/A * Returns a {@link SocketPermission} object representing the 0N/A * permission necessary to connect to the destination host and port. 0N/A * @exception IOException if an error occurs while computing 0N/A * @return a <code>SocketPermission</code> object representing the 0N/A * permission necessary to connect to the destination 0N/A * Returns the error stream if the connection failed 0N/A * but the server sent useful data nonetheless. The 0N/A * typical example is when an HTTP server responds 0N/A * with a 404, which will cause a FileNotFoundException 0N/A * to be thrown in connect, but the server sent an HTML 0N/A * help page with suggestions as to what to do. 0N/A * <p>This method will not cause a connection to be initiated. If 0N/A * the connection was not connected, or if the server did not have 0N/A * an error while connecting or if the server had an error but 0N/A * no error data was sent, this method will return null. This is 0N/A * @return an error stream if any, null if there have been no 0N/A * errors, the connection is not connected or the server sent no 0N/A * The response codes for HTTP, as of version 1.1. 0N/A // REMIND: do we want all these?? 0N/A // Others not here that we do want?? 0N/A /* 2XX: generally "OK" */ 0N/A * HTTP Status-Code 200: OK. 0N/A * HTTP Status-Code 201: Created. 0N/A * HTTP Status-Code 202: Accepted. 0N/A * HTTP Status-Code 203: Non-Authoritative Information. 0N/A * HTTP Status-Code 204: No Content. 0N/A * HTTP Status-Code 205: Reset Content. 0N/A * HTTP Status-Code 206: Partial Content. 0N/A * HTTP Status-Code 300: Multiple Choices. 0N/A * HTTP Status-Code 301: Moved Permanently. 0N/A * HTTP Status-Code 302: Temporary Redirect. 0N/A * HTTP Status-Code 303: See Other. 0N/A * HTTP Status-Code 304: Not Modified. 0N/A * HTTP Status-Code 305: Use Proxy. 0N/A /* 4XX: client error */ 0N/A * HTTP Status-Code 400: Bad Request. 0N/A * HTTP Status-Code 401: Unauthorized. 0N/A * HTTP Status-Code 402: Payment Required. 0N/A * HTTP Status-Code 403: Forbidden. 0N/A * HTTP Status-Code 404: Not Found. 0N/A * HTTP Status-Code 405: Method Not Allowed. 0N/A * HTTP Status-Code 406: Not Acceptable. 0N/A * HTTP Status-Code 407: Proxy Authentication Required. 0N/A * HTTP Status-Code 408: Request Time-Out. 0N/A * HTTP Status-Code 409: Conflict. 0N/A * HTTP Status-Code 410: Gone. 0N/A * HTTP Status-Code 411: Length Required. 0N/A * HTTP Status-Code 412: Precondition Failed. 0N/A * HTTP Status-Code 413: Request Entity Too Large. 0N/A * HTTP Status-Code 414: Request-URI Too Large. 0N/A * HTTP Status-Code 415: Unsupported Media Type. 0N/A /* 5XX: server error */ 0N/A * HTTP Status-Code 500: Internal Server Error. 0N/A * @deprecated it is misplaced and shouldn't have existed. 0N/A * HTTP Status-Code 500: Internal Server Error. 0N/A * HTTP Status-Code 501: Not Implemented. 0N/A * HTTP Status-Code 502: Bad Gateway. 0N/A * HTTP Status-Code 503: Service Unavailable. 0N/A * HTTP Status-Code 504: Gateway Timeout. 0N/A * HTTP Status-Code 505: HTTP Version Not Supported.