author | chegar |
Thu, 03 Mar 2011 16:44:44 +0000 | |
changeset 8562 | 72c0c44969c1 |
parent 7668 | d4a77089c587 |
child 8566 | e2df5f8fa082 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.security.Permission; |
|
31 |
import java.util.Date; |
|
32 |
||
33 |
/** |
|
34 |
* A URLConnection with support for HTTP-specific features. See |
|
35 |
* <A HREF="http://www.w3.org/pub/WWW/Protocols/"> the spec </A> for |
|
36 |
* details. |
|
37 |
* <p> |
|
38 |
* |
|
39 |
* Each HttpURLConnection instance is used to make a single request |
|
40 |
* but the underlying network connection to the HTTP server may be |
|
41 |
* transparently shared by other instances. Calling the close() methods |
|
42 |
* on the InputStream or OutputStream of an HttpURLConnection |
|
43 |
* after a request may free network resources associated with this |
|
44 |
* instance but has no effect on any shared persistent connection. |
|
45 |
* Calling the disconnect() method may close the underlying socket |
|
46 |
* if a persistent connection is otherwise idle at that time. |
|
47 |
* |
|
48 |
* <P>The HTTP protocol handler has a few settings that can be accessed through |
|
49 |
* System Properties. This covers |
|
50 |
* <a href="doc-files/net-properties.html#Proxies">Proxy settings</a> as well as |
|
51 |
* <a href="doc-files/net-properties.html#MiscHTTP"> various other settings</a>. |
|
52 |
* </P> |
|
53 |
* |
|
54 |
* @see java.net.HttpURLConnection#disconnect() |
|
55 |
* @since JDK1.1 |
|
56 |
*/ |
|
57 |
abstract public class HttpURLConnection extends URLConnection { |
|
58 |
/* instance variables */ |
|
59 |
||
60 |
/** |
|
61 |
* The HTTP method (GET,POST,PUT,etc.). |
|
62 |
*/ |
|
63 |
protected String method = "GET"; |
|
64 |
||
65 |
/** |
|
66 |
* The chunk-length when using chunked encoding streaming mode for output. |
|
67 |
* A value of <code>-1</code> means chunked encoding is disabled for output. |
|
68 |
* @since 1.5 |
|
69 |
*/ |
|
70 |
protected int chunkLength = -1; |
|
71 |
||
72 |
/** |
|
73 |
* The fixed content-length when using fixed-length streaming mode. |
|
74 |
* A value of <code>-1</code> means fixed-length streaming mode is disabled |
|
75 |
* for output. |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
76 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
77 |
* <P> <B>NOTE:</B> {@link #fixedContentLengthLong} is recommended instead |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
78 |
* of this field, as it allows larger content lengths to be set. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
79 |
* |
2 | 80 |
* @since 1.5 |
81 |
*/ |
|
82 |
protected int fixedContentLength = -1; |
|
83 |
||
84 |
/** |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
85 |
* The fixed content-length when using fixed-length streaming mode. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
86 |
* A value of {@code -1} means fixed-length streaming mode is disabled |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
87 |
* for output. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
88 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
89 |
* @since 1.7 |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
90 |
*/ |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
91 |
protected long fixedContentLengthLong = -1; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
92 |
|
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
93 |
/** |
2 | 94 |
* Returns the key for the <code>n</code><sup>th</sup> header field. |
95 |
* Some implementations may treat the <code>0</code><sup>th</sup> |
|
96 |
* header field as special, i.e. as the status line returned by the HTTP |
|
97 |
* server. In this case, {@link #getHeaderField(int) getHeaderField(0)} returns the status |
|
98 |
* line, but <code>getHeaderFieldKey(0)</code> returns null. |
|
99 |
* |
|
100 |
* @param n an index, where n >=0. |
|
101 |
* @return the key for the <code>n</code><sup>th</sup> header field, |
|
102 |
* or <code>null</code> if the key does not exist. |
|
103 |
*/ |
|
104 |
public String getHeaderFieldKey (int n) { |
|
105 |
return null; |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* This method is used to enable streaming of a HTTP request body |
|
110 |
* without internal buffering, when the content length is known in |
|
111 |
* advance. |
|
112 |
* <p> |
|
113 |
* An exception will be thrown if the application |
|
114 |
* attempts to write more data than the indicated |
|
115 |
* content-length, or if the application closes the OutputStream |
|
116 |
* before writing the indicated amount. |
|
117 |
* <p> |
|
118 |
* When output streaming is enabled, authentication |
|
119 |
* and redirection cannot be handled automatically. |
|
120 |
* A HttpRetryException will be thrown when reading |
|
121 |
* the response if authentication or redirection are required. |
|
122 |
* This exception can be queried for the details of the error. |
|
123 |
* <p> |
|
124 |
* This method must be called before the URLConnection is connected. |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
125 |
* <p> |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
126 |
* <B>NOTE:</B> {@link #setFixedLengthStreamingMode(long)} is recommended |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
127 |
* instead of this method as it allows larger content lengths to be set. |
2 | 128 |
* |
129 |
* @param contentLength The number of bytes which will be written |
|
130 |
* to the OutputStream. |
|
131 |
* |
|
132 |
* @throws IllegalStateException if URLConnection is already connected |
|
133 |
* or if a different streaming mode is already enabled. |
|
134 |
* |
|
135 |
* @throws IllegalArgumentException if a content length less than |
|
136 |
* zero is specified. |
|
137 |
* |
|
138 |
* @see #setChunkedStreamingMode(int) |
|
139 |
* @since 1.5 |
|
140 |
*/ |
|
141 |
public void setFixedLengthStreamingMode (int contentLength) { |
|
142 |
if (connected) { |
|
143 |
throw new IllegalStateException ("Already connected"); |
|
144 |
} |
|
145 |
if (chunkLength != -1) { |
|
146 |
throw new IllegalStateException ("Chunked encoding streaming mode set"); |
|
147 |
} |
|
148 |
if (contentLength < 0) { |
|
149 |
throw new IllegalArgumentException ("invalid content length"); |
|
150 |
} |
|
151 |
fixedContentLength = contentLength; |
|
152 |
} |
|
153 |
||
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
154 |
/** |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
155 |
* This method is used to enable streaming of a HTTP request body |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
156 |
* without internal buffering, when the content length is known in |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
157 |
* advance. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
158 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
159 |
* <P> An exception will be thrown if the application attempts to write |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
160 |
* more data than the indicated content-length, or if the application |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
161 |
* closes the OutputStream before writing the indicated amount. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
162 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
163 |
* <P> When output streaming is enabled, authentication and redirection |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
164 |
* cannot be handled automatically. A {@linkplain HttpRetryException} will |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
165 |
* be thrown when reading the response if authentication or redirection |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
166 |
* are required. This exception can be queried for the details of the |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
167 |
* error. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
168 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
169 |
* <P> This method must be called before the URLConnection is connected. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
170 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
171 |
* <P> The content length set by invoking this method takes precedence |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
172 |
* over any value set by {@link #setFixedLengthStreamingMode(int)}. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
173 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
174 |
* @param contentLength |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
175 |
* The number of bytes which will be written to the OutputStream. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
176 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
177 |
* @throws IllegalStateException |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
178 |
* if URLConnection is already connected or if a different |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
179 |
* streaming mode is already enabled. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
180 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
181 |
* @throws IllegalArgumentException |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
182 |
* if a content length less than zero is specified. |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
183 |
* |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
184 |
* @since 1.7 |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
185 |
*/ |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
186 |
public void setFixedLengthStreamingMode(long contentLength) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
187 |
if (connected) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
188 |
throw new IllegalStateException("Already connected"); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
189 |
} |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
190 |
if (chunkLength != -1) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
191 |
throw new IllegalStateException( |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
192 |
"Chunked encoding streaming mode set"); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
193 |
} |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
194 |
if (contentLength < 0) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
195 |
throw new IllegalArgumentException("invalid content length"); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
196 |
} |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
197 |
fixedContentLengthLong = contentLength; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
198 |
} |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
199 |
|
2 | 200 |
/* Default chunk size (including chunk header) if not specified; |
201 |
* we want to keep this in sync with the one defined in |
|
202 |
* sun.net.www.http.ChunkedOutputStream |
|
203 |
*/ |
|
204 |
private static final int DEFAULT_CHUNK_SIZE = 4096; |
|
205 |
||
206 |
/** |
|
207 |
* This method is used to enable streaming of a HTTP request body |
|
208 |
* without internal buffering, when the content length is <b>not</b> |
|
209 |
* known in advance. In this mode, chunked transfer encoding |
|
210 |
* is used to send the request body. Note, not all HTTP servers |
|
211 |
* support this mode. |
|
212 |
* <p> |
|
213 |
* When output streaming is enabled, authentication |
|
214 |
* and redirection cannot be handled automatically. |
|
215 |
* A HttpRetryException will be thrown when reading |
|
216 |
* the response if authentication or redirection are required. |
|
217 |
* This exception can be queried for the details of the error. |
|
218 |
* <p> |
|
219 |
* This method must be called before the URLConnection is connected. |
|
220 |
* |
|
221 |
* @param chunklen The number of bytes to write in each chunk. |
|
222 |
* If chunklen is less than or equal to zero, a default |
|
223 |
* value will be used. |
|
224 |
* |
|
225 |
* @throws IllegalStateException if URLConnection is already connected |
|
226 |
* or if a different streaming mode is already enabled. |
|
227 |
* |
|
228 |
* @see #setFixedLengthStreamingMode(int) |
|
229 |
* @since 1.5 |
|
230 |
*/ |
|
231 |
public void setChunkedStreamingMode (int chunklen) { |
|
232 |
if (connected) { |
|
233 |
throw new IllegalStateException ("Can't set streaming mode: already connected"); |
|
234 |
} |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
2
diff
changeset
|
235 |
if (fixedContentLength != -1 || fixedContentLengthLong != -1) { |
2 | 236 |
throw new IllegalStateException ("Fixed length streaming mode set"); |
237 |
} |
|
238 |
chunkLength = chunklen <=0? DEFAULT_CHUNK_SIZE : chunklen; |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Returns the value for the <code>n</code><sup>th</sup> header field. |
|
243 |
* Some implementations may treat the <code>0</code><sup>th</sup> |
|
244 |
* header field as special, i.e. as the status line returned by the HTTP |
|
245 |
* server. |
|
246 |
* <p> |
|
247 |
* This method can be used in conjunction with the |
|
248 |
* {@link #getHeaderFieldKey getHeaderFieldKey} method to iterate through all |
|
249 |
* the headers in the message. |
|
250 |
* |
|
251 |
* @param n an index, where n>=0. |
|
252 |
* @return the value of the <code>n</code><sup>th</sup> header field, |
|
253 |
* or <code>null</code> if the value does not exist. |
|
254 |
* @see java.net.HttpURLConnection#getHeaderFieldKey(int) |
|
255 |
*/ |
|
256 |
public String getHeaderField(int n) { |
|
257 |
return null; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* An <code>int</code> representing the three digit HTTP Status-Code. |
|
262 |
* <ul> |
|
263 |
* <li> 1xx: Informational |
|
264 |
* <li> 2xx: Success |
|
265 |
* <li> 3xx: Redirection |
|
266 |
* <li> 4xx: Client Error |
|
267 |
* <li> 5xx: Server Error |
|
268 |
* </ul> |
|
269 |
*/ |
|
270 |
protected int responseCode = -1; |
|
271 |
||
272 |
/** |
|
273 |
* The HTTP response message. |
|
274 |
*/ |
|
275 |
protected String responseMessage = null; |
|
276 |
||
277 |
/* static variables */ |
|
278 |
||
279 |
/* do we automatically follow redirects? The default is true. */ |
|
280 |
private static boolean followRedirects = true; |
|
281 |
||
282 |
/** |
|
283 |
* If <code>true</code>, the protocol will automatically follow redirects. |
|
284 |
* If <code>false</code>, the protocol will not automatically follow |
|
285 |
* redirects. |
|
286 |
* <p> |
|
287 |
* This field is set by the <code>setInstanceFollowRedirects</code> |
|
288 |
* method. Its value is returned by the <code>getInstanceFollowRedirects</code> |
|
289 |
* method. |
|
290 |
* <p> |
|
291 |
* Its default value is based on the value of the static followRedirects |
|
292 |
* at HttpURLConnection construction time. |
|
293 |
* |
|
294 |
* @see java.net.HttpURLConnection#setInstanceFollowRedirects(boolean) |
|
295 |
* @see java.net.HttpURLConnection#getInstanceFollowRedirects() |
|
296 |
* @see java.net.HttpURLConnection#setFollowRedirects(boolean) |
|
297 |
*/ |
|
298 |
protected boolean instanceFollowRedirects = followRedirects; |
|
299 |
||
300 |
/* valid HTTP methods */ |
|
301 |
private static final String[] methods = { |
|
302 |
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE" |
|
303 |
}; |
|
304 |
||
305 |
/** |
|
306 |
* Constructor for the HttpURLConnection. |
|
307 |
* @param u the URL |
|
308 |
*/ |
|
309 |
protected HttpURLConnection (URL u) { |
|
310 |
super(u); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Sets whether HTTP redirects (requests with response code 3xx) should |
|
315 |
* be automatically followed by this class. True by default. Applets |
|
316 |
* cannot change this variable. |
|
317 |
* <p> |
|
318 |
* If there is a security manager, this method first calls |
|
319 |
* the security manager's <code>checkSetFactory</code> method |
|
320 |
* to ensure the operation is allowed. |
|
321 |
* This could result in a SecurityException. |
|
322 |
* |
|
323 |
* @param set a <code>boolean</code> indicating whether or not |
|
324 |
* to follow HTTP redirects. |
|
325 |
* @exception SecurityException if a security manager exists and its |
|
326 |
* <code>checkSetFactory</code> method doesn't |
|
327 |
* allow the operation. |
|
328 |
* @see SecurityManager#checkSetFactory |
|
329 |
* @see #getFollowRedirects() |
|
330 |
*/ |
|
331 |
public static void setFollowRedirects(boolean set) { |
|
332 |
SecurityManager sec = System.getSecurityManager(); |
|
333 |
if (sec != null) { |
|
334 |
// seems to be the best check here... |
|
335 |
sec.checkSetFactory(); |
|
336 |
} |
|
337 |
followRedirects = set; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Returns a <code>boolean</code> indicating |
|
342 |
* whether or not HTTP redirects (3xx) should |
|
343 |
* be automatically followed. |
|
344 |
* |
|
345 |
* @return <code>true</code> if HTTP redirects should |
|
346 |
* be automatically followed, <tt>false</tt> if not. |
|
347 |
* @see #setFollowRedirects(boolean) |
|
348 |
*/ |
|
349 |
public static boolean getFollowRedirects() { |
|
350 |
return followRedirects; |
|
351 |
} |
|
352 |
||
353 |
/** |
|
354 |
* Sets whether HTTP redirects (requests with response code 3xx) should |
|
355 |
* be automatically followed by this <code>HttpURLConnection</code> |
|
356 |
* instance. |
|
357 |
* <p> |
|
358 |
* The default value comes from followRedirects, which defaults to |
|
359 |
* true. |
|
360 |
* |
|
361 |
* @param followRedirects a <code>boolean</code> indicating |
|
362 |
* whether or not to follow HTTP redirects. |
|
363 |
* |
|
364 |
* @see java.net.HttpURLConnection#instanceFollowRedirects |
|
365 |
* @see #getInstanceFollowRedirects |
|
366 |
* @since 1.3 |
|
367 |
*/ |
|
368 |
public void setInstanceFollowRedirects(boolean followRedirects) { |
|
369 |
instanceFollowRedirects = followRedirects; |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Returns the value of this <code>HttpURLConnection</code>'s |
|
374 |
* <code>instanceFollowRedirects</code> field. |
|
375 |
* |
|
376 |
* @return the value of this <code>HttpURLConnection</code>'s |
|
377 |
* <code>instanceFollowRedirects</code> field. |
|
378 |
* @see java.net.HttpURLConnection#instanceFollowRedirects |
|
379 |
* @see #setInstanceFollowRedirects(boolean) |
|
380 |
* @since 1.3 |
|
381 |
*/ |
|
382 |
public boolean getInstanceFollowRedirects() { |
|
383 |
return instanceFollowRedirects; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Set the method for the URL request, one of: |
|
388 |
* <UL> |
|
389 |
* <LI>GET |
|
390 |
* <LI>POST |
|
391 |
* <LI>HEAD |
|
392 |
* <LI>OPTIONS |
|
393 |
* <LI>PUT |
|
394 |
* <LI>DELETE |
|
395 |
* <LI>TRACE |
|
396 |
* </UL> are legal, subject to protocol restrictions. The default |
|
397 |
* method is GET. |
|
398 |
* |
|
399 |
* @param method the HTTP method |
|
400 |
* @exception ProtocolException if the method cannot be reset or if |
|
401 |
* the requested method isn't valid for HTTP. |
|
6875
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
402 |
* @exception SecurityException if a security manager is set and the |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
403 |
* "allowHttpTrace" NetPermission is not granted. |
2 | 404 |
* @see #getRequestMethod() |
405 |
*/ |
|
406 |
public void setRequestMethod(String method) throws ProtocolException { |
|
407 |
if (connected) { |
|
408 |
throw new ProtocolException("Can't reset method: already connected"); |
|
409 |
} |
|
410 |
// This restriction will prevent people from using this class to |
|
411 |
// experiment w/ new HTTP methods using java. But it should |
|
412 |
// be placed for security - the request String could be |
|
413 |
// arbitrarily long. |
|
414 |
||
415 |
for (int i = 0; i < methods.length; i++) { |
|
416 |
if (methods[i].equals(method)) { |
|
6875
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
417 |
if (method.equals("TRACE")) { |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
418 |
SecurityManager s = System.getSecurityManager(); |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
419 |
if (s != null) { |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
420 |
s.checkPermission(new NetPermission("allowHttpTrace")); |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
421 |
} |
81d6ec3397e5
6981426: limit use of TRACE method in HttpURLConnection
michaelm
parents:
5506
diff
changeset
|
422 |
} |
2 | 423 |
this.method = method; |
424 |
return; |
|
425 |
} |
|
426 |
} |
|
427 |
throw new ProtocolException("Invalid HTTP method: " + method); |
|
428 |
} |
|
429 |
||
430 |
/** |
|
431 |
* Get the request method. |
|
432 |
* @return the HTTP request method |
|
433 |
* @see #setRequestMethod(java.lang.String) |
|
434 |
*/ |
|
435 |
public String getRequestMethod() { |
|
436 |
return method; |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Gets the status code from an HTTP response message. |
|
441 |
* For example, in the case of the following status lines: |
|
442 |
* <PRE> |
|
443 |
* HTTP/1.0 200 OK |
|
444 |
* HTTP/1.0 401 Unauthorized |
|
445 |
* </PRE> |
|
446 |
* It will return 200 and 401 respectively. |
|
447 |
* Returns -1 if no code can be discerned |
|
448 |
* from the response (i.e., the response is not valid HTTP). |
|
449 |
* @throws IOException if an error occurred connecting to the server. |
|
450 |
* @return the HTTP Status-Code, or -1 |
|
451 |
*/ |
|
452 |
public int getResponseCode() throws IOException { |
|
453 |
/* |
|
454 |
* We're got the response code already |
|
455 |
*/ |
|
456 |
if (responseCode != -1) { |
|
457 |
return responseCode; |
|
458 |
} |
|
459 |
||
460 |
/* |
|
461 |
* Ensure that we have connected to the server. Record |
|
462 |
* exception as we need to re-throw it if there isn't |
|
463 |
* a status line. |
|
464 |
*/ |
|
465 |
Exception exc = null; |
|
466 |
try { |
|
467 |
getInputStream(); |
|
468 |
} catch (Exception e) { |
|
469 |
exc = e; |
|
470 |
} |
|
471 |
||
472 |
/* |
|
473 |
* If we can't a status-line then re-throw any exception |
|
474 |
* that getInputStream threw. |
|
475 |
*/ |
|
476 |
String statusLine = getHeaderField(0); |
|
477 |
if (statusLine == null) { |
|
478 |
if (exc != null) { |
|
479 |
if (exc instanceof RuntimeException) |
|
480 |
throw (RuntimeException)exc; |
|
481 |
else |
|
482 |
throw (IOException)exc; |
|
483 |
} |
|
484 |
return -1; |
|
485 |
} |
|
486 |
||
487 |
/* |
|
488 |
* Examine the status-line - should be formatted as per |
|
489 |
* section 6.1 of RFC 2616 :- |
|
490 |
* |
|
491 |
* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase |
|
492 |
* |
|
493 |
* If status line can't be parsed return -1. |
|
494 |
*/ |
|
495 |
if (statusLine.startsWith("HTTP/1.")) { |
|
496 |
int codePos = statusLine.indexOf(' '); |
|
497 |
if (codePos > 0) { |
|
498 |
||
499 |
int phrasePos = statusLine.indexOf(' ', codePos+1); |
|
500 |
if (phrasePos > 0 && phrasePos < statusLine.length()) { |
|
501 |
responseMessage = statusLine.substring(phrasePos+1); |
|
502 |
} |
|
503 |
||
504 |
// deviation from RFC 2616 - don't reject status line |
|
505 |
// if SP Reason-Phrase is not included. |
|
506 |
if (phrasePos < 0) |
|
507 |
phrasePos = statusLine.length(); |
|
508 |
||
509 |
try { |
|
510 |
responseCode = Integer.parseInt |
|
511 |
(statusLine.substring(codePos+1, phrasePos)); |
|
512 |
return responseCode; |
|
513 |
} catch (NumberFormatException e) { } |
|
514 |
} |
|
515 |
} |
|
516 |
return -1; |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Gets the HTTP response message, if any, returned along with the |
|
521 |
* response code from a server. From responses like: |
|
522 |
* <PRE> |
|
523 |
* HTTP/1.0 200 OK |
|
524 |
* HTTP/1.0 404 Not Found |
|
525 |
* </PRE> |
|
526 |
* Extracts the Strings "OK" and "Not Found" respectively. |
|
527 |
* Returns null if none could be discerned from the responses |
|
528 |
* (the result was not valid HTTP). |
|
529 |
* @throws IOException if an error occurred connecting to the server. |
|
530 |
* @return the HTTP response message, or <code>null</code> |
|
531 |
*/ |
|
532 |
public String getResponseMessage() throws IOException { |
|
533 |
getResponseCode(); |
|
534 |
return responseMessage; |
|
535 |
} |
|
536 |
||
537 |
public long getHeaderFieldDate(String name, long Default) { |
|
538 |
String dateString = getHeaderField(name); |
|
539 |
try { |
|
540 |
if (dateString.indexOf("GMT") == -1) { |
|
541 |
dateString = dateString+" GMT"; |
|
542 |
} |
|
543 |
return Date.parse(dateString); |
|
544 |
} catch (Exception e) { |
|
545 |
} |
|
546 |
return Default; |
|
547 |
} |
|
548 |
||
549 |
||
550 |
/** |
|
551 |
* Indicates that other requests to the server |
|
552 |
* are unlikely in the near future. Calling disconnect() |
|
553 |
* should not imply that this HttpURLConnection |
|
554 |
* instance can be reused for other requests. |
|
555 |
*/ |
|
556 |
public abstract void disconnect(); |
|
557 |
||
558 |
/** |
|
559 |
* Indicates if the connection is going through a proxy. |
|
560 |
* @return a boolean indicating if the connection is |
|
561 |
* using a proxy. |
|
562 |
*/ |
|
563 |
public abstract boolean usingProxy(); |
|
564 |
||
565 |
/** |
|
566 |
* Returns a {@link SocketPermission} object representing the |
|
567 |
* permission necessary to connect to the destination host and port. |
|
568 |
* |
|
569 |
* @exception IOException if an error occurs while computing |
|
570 |
* the permission. |
|
571 |
* |
|
572 |
* @return a <code>SocketPermission</code> object representing the |
|
573 |
* permission necessary to connect to the destination |
|
574 |
* host and port. |
|
575 |
*/ |
|
576 |
public Permission getPermission() throws IOException { |
|
577 |
int port = url.getPort(); |
|
578 |
port = port < 0 ? 80 : port; |
|
579 |
String host = url.getHost() + ":" + port; |
|
580 |
Permission permission = new SocketPermission(host, "connect"); |
|
581 |
return permission; |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* Returns the error stream if the connection failed |
|
586 |
* but the server sent useful data nonetheless. The |
|
587 |
* typical example is when an HTTP server responds |
|
588 |
* with a 404, which will cause a FileNotFoundException |
|
589 |
* to be thrown in connect, but the server sent an HTML |
|
590 |
* help page with suggestions as to what to do. |
|
591 |
* |
|
592 |
* <p>This method will not cause a connection to be initiated. If |
|
593 |
* the connection was not connected, or if the server did not have |
|
594 |
* an error while connecting or if the server had an error but |
|
595 |
* no error data was sent, this method will return null. This is |
|
596 |
* the default. |
|
597 |
* |
|
598 |
* @return an error stream if any, null if there have been no |
|
599 |
* errors, the connection is not connected or the server sent no |
|
600 |
* useful data. |
|
601 |
*/ |
|
602 |
public InputStream getErrorStream() { |
|
603 |
return null; |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* The response codes for HTTP, as of version 1.1. |
|
608 |
*/ |
|
609 |
||
610 |
// REMIND: do we want all these?? |
|
611 |
// Others not here that we do want?? |
|
612 |
||
613 |
/* 2XX: generally "OK" */ |
|
614 |
||
615 |
/** |
|
616 |
* HTTP Status-Code 200: OK. |
|
617 |
*/ |
|
618 |
public static final int HTTP_OK = 200; |
|
619 |
||
620 |
/** |
|
621 |
* HTTP Status-Code 201: Created. |
|
622 |
*/ |
|
623 |
public static final int HTTP_CREATED = 201; |
|
624 |
||
625 |
/** |
|
626 |
* HTTP Status-Code 202: Accepted. |
|
627 |
*/ |
|
628 |
public static final int HTTP_ACCEPTED = 202; |
|
629 |
||
630 |
/** |
|
631 |
* HTTP Status-Code 203: Non-Authoritative Information. |
|
632 |
*/ |
|
633 |
public static final int HTTP_NOT_AUTHORITATIVE = 203; |
|
634 |
||
635 |
/** |
|
636 |
* HTTP Status-Code 204: No Content. |
|
637 |
*/ |
|
638 |
public static final int HTTP_NO_CONTENT = 204; |
|
639 |
||
640 |
/** |
|
641 |
* HTTP Status-Code 205: Reset Content. |
|
642 |
*/ |
|
643 |
public static final int HTTP_RESET = 205; |
|
644 |
||
645 |
/** |
|
646 |
* HTTP Status-Code 206: Partial Content. |
|
647 |
*/ |
|
648 |
public static final int HTTP_PARTIAL = 206; |
|
649 |
||
650 |
/* 3XX: relocation/redirect */ |
|
651 |
||
652 |
/** |
|
653 |
* HTTP Status-Code 300: Multiple Choices. |
|
654 |
*/ |
|
655 |
public static final int HTTP_MULT_CHOICE = 300; |
|
656 |
||
657 |
/** |
|
658 |
* HTTP Status-Code 301: Moved Permanently. |
|
659 |
*/ |
|
660 |
public static final int HTTP_MOVED_PERM = 301; |
|
661 |
||
662 |
/** |
|
663 |
* HTTP Status-Code 302: Temporary Redirect. |
|
664 |
*/ |
|
665 |
public static final int HTTP_MOVED_TEMP = 302; |
|
666 |
||
667 |
/** |
|
668 |
* HTTP Status-Code 303: See Other. |
|
669 |
*/ |
|
670 |
public static final int HTTP_SEE_OTHER = 303; |
|
671 |
||
672 |
/** |
|
673 |
* HTTP Status-Code 304: Not Modified. |
|
674 |
*/ |
|
675 |
public static final int HTTP_NOT_MODIFIED = 304; |
|
676 |
||
677 |
/** |
|
678 |
* HTTP Status-Code 305: Use Proxy. |
|
679 |
*/ |
|
680 |
public static final int HTTP_USE_PROXY = 305; |
|
681 |
||
682 |
/* 4XX: client error */ |
|
683 |
||
684 |
/** |
|
685 |
* HTTP Status-Code 400: Bad Request. |
|
686 |
*/ |
|
687 |
public static final int HTTP_BAD_REQUEST = 400; |
|
688 |
||
689 |
/** |
|
690 |
* HTTP Status-Code 401: Unauthorized. |
|
691 |
*/ |
|
692 |
public static final int HTTP_UNAUTHORIZED = 401; |
|
693 |
||
694 |
/** |
|
695 |
* HTTP Status-Code 402: Payment Required. |
|
696 |
*/ |
|
697 |
public static final int HTTP_PAYMENT_REQUIRED = 402; |
|
698 |
||
699 |
/** |
|
700 |
* HTTP Status-Code 403: Forbidden. |
|
701 |
*/ |
|
702 |
public static final int HTTP_FORBIDDEN = 403; |
|
703 |
||
704 |
/** |
|
705 |
* HTTP Status-Code 404: Not Found. |
|
706 |
*/ |
|
707 |
public static final int HTTP_NOT_FOUND = 404; |
|
708 |
||
709 |
/** |
|
710 |
* HTTP Status-Code 405: Method Not Allowed. |
|
711 |
*/ |
|
712 |
public static final int HTTP_BAD_METHOD = 405; |
|
713 |
||
714 |
/** |
|
715 |
* HTTP Status-Code 406: Not Acceptable. |
|
716 |
*/ |
|
717 |
public static final int HTTP_NOT_ACCEPTABLE = 406; |
|
718 |
||
719 |
/** |
|
720 |
* HTTP Status-Code 407: Proxy Authentication Required. |
|
721 |
*/ |
|
722 |
public static final int HTTP_PROXY_AUTH = 407; |
|
723 |
||
724 |
/** |
|
725 |
* HTTP Status-Code 408: Request Time-Out. |
|
726 |
*/ |
|
727 |
public static final int HTTP_CLIENT_TIMEOUT = 408; |
|
728 |
||
729 |
/** |
|
730 |
* HTTP Status-Code 409: Conflict. |
|
731 |
*/ |
|
732 |
public static final int HTTP_CONFLICT = 409; |
|
733 |
||
734 |
/** |
|
735 |
* HTTP Status-Code 410: Gone. |
|
736 |
*/ |
|
737 |
public static final int HTTP_GONE = 410; |
|
738 |
||
739 |
/** |
|
740 |
* HTTP Status-Code 411: Length Required. |
|
741 |
*/ |
|
742 |
public static final int HTTP_LENGTH_REQUIRED = 411; |
|
743 |
||
744 |
/** |
|
745 |
* HTTP Status-Code 412: Precondition Failed. |
|
746 |
*/ |
|
747 |
public static final int HTTP_PRECON_FAILED = 412; |
|
748 |
||
749 |
/** |
|
750 |
* HTTP Status-Code 413: Request Entity Too Large. |
|
751 |
*/ |
|
752 |
public static final int HTTP_ENTITY_TOO_LARGE = 413; |
|
753 |
||
754 |
/** |
|
755 |
* HTTP Status-Code 414: Request-URI Too Large. |
|
756 |
*/ |
|
757 |
public static final int HTTP_REQ_TOO_LONG = 414; |
|
758 |
||
759 |
/** |
|
760 |
* HTTP Status-Code 415: Unsupported Media Type. |
|
761 |
*/ |
|
762 |
public static final int HTTP_UNSUPPORTED_TYPE = 415; |
|
763 |
||
764 |
/* 5XX: server error */ |
|
765 |
||
766 |
/** |
|
767 |
* HTTP Status-Code 500: Internal Server Error. |
|
768 |
* @deprecated it is misplaced and shouldn't have existed. |
|
769 |
*/ |
|
770 |
@Deprecated |
|
771 |
public static final int HTTP_SERVER_ERROR = 500; |
|
772 |
||
773 |
/** |
|
774 |
* HTTP Status-Code 500: Internal Server Error. |
|
775 |
*/ |
|
776 |
public static final int HTTP_INTERNAL_ERROR = 500; |
|
777 |
||
778 |
/** |
|
779 |
* HTTP Status-Code 501: Not Implemented. |
|
780 |
*/ |
|
781 |
public static final int HTTP_NOT_IMPLEMENTED = 501; |
|
782 |
||
783 |
/** |
|
784 |
* HTTP Status-Code 502: Bad Gateway. |
|
785 |
*/ |
|
786 |
public static final int HTTP_BAD_GATEWAY = 502; |
|
787 |
||
788 |
/** |
|
789 |
* HTTP Status-Code 503: Service Unavailable. |
|
790 |
*/ |
|
791 |
public static final int HTTP_UNAVAILABLE = 503; |
|
792 |
||
793 |
/** |
|
794 |
* HTTP Status-Code 504: Gateway Timeout. |
|
795 |
*/ |
|
796 |
public static final int HTTP_GATEWAY_TIMEOUT = 504; |
|
797 |
||
798 |
/** |
|
799 |
* HTTP Status-Code 505: HTTP Version Not Supported. |
|
800 |
*/ |
|
801 |
public static final int HTTP_VERSION = 505; |
|
802 |
||
803 |
} |