38 import java.security.AccessController; |
38 import java.security.AccessController; |
39 import sun.security.util.SecurityConstants; |
39 import sun.security.util.SecurityConstants; |
40 import sun.net.www.MessageHeader; |
40 import sun.net.www.MessageHeader; |
41 |
41 |
42 /** |
42 /** |
43 * The abstract class <code>URLConnection</code> is the superclass |
43 * The abstract class {@code URLConnection} is the superclass |
44 * of all classes that represent a communications link between the |
44 * of all classes that represent a communications link between the |
45 * application and a URL. Instances of this class can be used both to |
45 * application and a URL. Instances of this class can be used both to |
46 * read from and to write to the resource referenced by the URL. In |
46 * read from and to write to the resource referenced by the URL. In |
47 * general, creating a connection to a URL is a multistep process: |
47 * general, creating a connection to a URL is a multistep process: |
48 * <p> |
48 * <p> |
49 * <center><table border=2 summary="Describes the process of creating a connection to a URL: openConnection() and connect() over time."> |
49 * <center><table border=2 summary="Describes the process of creating a connection to a URL: openConnection() and connect() over time."> |
50 * <tr><th><code>openConnection()</code></th> |
50 * <tr><th>{@code openConnection()}</th> |
51 * <th><code>connect()</code></th></tr> |
51 * <th>{@code connect()}</th></tr> |
52 * <tr><td>Manipulate parameters that affect the connection to the remote |
52 * <tr><td>Manipulate parameters that affect the connection to the remote |
53 * resource.</td> |
53 * resource.</td> |
54 * <td>Interact with the resource; query header fields and |
54 * <td>Interact with the resource; query header fields and |
55 * contents.</td></tr> |
55 * contents.</td></tr> |
56 * </table> |
56 * </table> |
57 * ----------------------------> |
57 * ----------------------------> |
58 * <br>time</center> |
58 * <br>time</center> |
59 * |
59 * |
60 * <ol> |
60 * <ol> |
61 * <li>The connection object is created by invoking the |
61 * <li>The connection object is created by invoking the |
62 * <code>openConnection</code> method on a URL. |
62 * {@code openConnection} method on a URL. |
63 * <li>The setup parameters and general request properties are manipulated. |
63 * <li>The setup parameters and general request properties are manipulated. |
64 * <li>The actual connection to the remote object is made, using the |
64 * <li>The actual connection to the remote object is made, using the |
65 * <code>connect</code> method. |
65 * {@code connect} method. |
66 * <li>The remote object becomes available. The header fields and the contents |
66 * <li>The remote object becomes available. The header fields and the contents |
67 * of the remote object can be accessed. |
67 * of the remote object can be accessed. |
68 * </ol> |
68 * </ol> |
69 * <p> |
69 * <p> |
70 * The setup parameters are modified using the following methods: |
70 * The setup parameters are modified using the following methods: |
71 * <ul> |
71 * <ul> |
72 * <li><code>setAllowUserInteraction</code> |
72 * <li>{@code setAllowUserInteraction} |
73 * <li><code>setDoInput</code> |
73 * <li>{@code setDoInput} |
74 * <li><code>setDoOutput</code> |
74 * <li>{@code setDoOutput} |
75 * <li><code>setIfModifiedSince</code> |
75 * <li>{@code setIfModifiedSince} |
76 * <li><code>setUseCaches</code> |
76 * <li>{@code setUseCaches} |
77 * </ul> |
77 * </ul> |
78 * <p> |
78 * <p> |
79 * and the general request properties are modified using the method: |
79 * and the general request properties are modified using the method: |
80 * <ul> |
80 * <ul> |
81 * <li><code>setRequestProperty</code> |
81 * <li>{@code setRequestProperty} |
82 * </ul> |
82 * </ul> |
83 * <p> |
83 * <p> |
84 * Default values for the <code>AllowUserInteraction</code> and |
84 * Default values for the {@code AllowUserInteraction} and |
85 * <code>UseCaches</code> parameters can be set using the methods |
85 * {@code UseCaches} parameters can be set using the methods |
86 * <code>setDefaultAllowUserInteraction</code> and |
86 * {@code setDefaultAllowUserInteraction} and |
87 * <code>setDefaultUseCaches</code>. |
87 * {@code setDefaultUseCaches}. |
88 * <p> |
88 * <p> |
89 * Each of the above <code>set</code> methods has a corresponding |
89 * Each of the above {@code set} methods has a corresponding |
90 * <code>get</code> method to retrieve the value of the parameter or |
90 * {@code get} method to retrieve the value of the parameter or |
91 * general request property. The specific parameters and general |
91 * general request property. The specific parameters and general |
92 * request properties that are applicable are protocol specific. |
92 * request properties that are applicable are protocol specific. |
93 * <p> |
93 * <p> |
94 * The following methods are used to access the header fields and |
94 * The following methods are used to access the header fields and |
95 * the contents after the connection is made to the remote object: |
95 * the contents after the connection is made to the remote object: |
96 * <ul> |
96 * <ul> |
97 * <li><code>getContent</code> |
97 * <li>{@code getContent} |
98 * <li><code>getHeaderField</code> |
98 * <li>{@code getHeaderField} |
99 * <li><code>getInputStream</code> |
99 * <li>{@code getInputStream} |
100 * <li><code>getOutputStream</code> |
100 * <li>{@code getOutputStream} |
101 * </ul> |
101 * </ul> |
102 * <p> |
102 * <p> |
103 * Certain header fields are accessed frequently. The methods: |
103 * Certain header fields are accessed frequently. The methods: |
104 * <ul> |
104 * <ul> |
105 * <li><code>getContentEncoding</code> |
105 * <li>{@code getContentEncoding} |
106 * <li><code>getContentLength</code> |
106 * <li>{@code getContentLength} |
107 * <li><code>getContentType</code> |
107 * <li>{@code getContentType} |
108 * <li><code>getDate</code> |
108 * <li>{@code getDate} |
109 * <li><code>getExpiration</code> |
109 * <li>{@code getExpiration} |
110 * <li><code>getLastModifed</code> |
110 * <li>{@code getLastModifed} |
111 * </ul> |
111 * </ul> |
112 * <p> |
112 * <p> |
113 * provide convenient access to these fields. The |
113 * provide convenient access to these fields. The |
114 * <code>getContentType</code> method is used by the |
114 * {@code getContentType} method is used by the |
115 * <code>getContent</code> method to determine the type of the remote |
115 * {@code getContent} method to determine the type of the remote |
116 * object; subclasses may find it convenient to override the |
116 * object; subclasses may find it convenient to override the |
117 * <code>getContentType</code> method. |
117 * {@code getContentType} method. |
118 * <p> |
118 * <p> |
119 * In the common case, all of the pre-connection parameters and |
119 * In the common case, all of the pre-connection parameters and |
120 * general request properties can be ignored: the pre-connection |
120 * general request properties can be ignored: the pre-connection |
121 * parameters and request properties default to sensible values. For |
121 * parameters and request properties default to sensible values. For |
122 * most clients of this interface, there are only two interesting |
122 * most clients of this interface, there are only two interesting |
123 * methods: <code>getInputStream</code> and <code>getContent</code>, |
123 * methods: {@code getInputStream} and {@code getContent}, |
124 * which are mirrored in the <code>URL</code> class by convenience methods. |
124 * which are mirrored in the {@code URL} class by convenience methods. |
125 * <p> |
125 * <p> |
126 * More information on the request properties and header fields of |
126 * More information on the request properties and header fields of |
127 * an <code>http</code> connection can be found at: |
127 * an {@code http} connection can be found at: |
128 * <blockquote><pre> |
128 * <blockquote><pre> |
129 * <a href="http://www.ietf.org/rfc/rfc2616.txt">http://www.ietf.org/rfc/rfc2616.txt</a> |
129 * <a href="http://www.ietf.org/rfc/rfc2616.txt">http://www.ietf.org/rfc/rfc2616.txt</a> |
130 * </pre></blockquote> |
130 * </pre></blockquote> |
131 * |
131 * |
132 * Invoking the <tt>close()</tt> methods on the <tt>InputStream</tt> or <tt>OutputStream</tt> of an |
132 * Invoking the {@code close()} methods on the {@code InputStream} or {@code OutputStream} of an |
133 * <tt>URLConnection</tt> after a request may free network resources associated with this |
133 * {@code URLConnection} after a request may free network resources associated with this |
134 * instance, unless particular protocol specifications specify different behaviours |
134 * instance, unless particular protocol specifications specify different behaviours |
135 * for it. |
135 * for it. |
136 * |
136 * |
137 * @author James Gosling |
137 * @author James Gosling |
138 * @see java.net.URL#openConnection() |
138 * @see java.net.URL#openConnection() |
162 /** |
162 /** |
163 * The URL represents the remote object on the World Wide Web to |
163 * The URL represents the remote object on the World Wide Web to |
164 * which this connection is opened. |
164 * which this connection is opened. |
165 * <p> |
165 * <p> |
166 * The value of this field can be accessed by the |
166 * The value of this field can be accessed by the |
167 * <code>getURL</code> method. |
167 * {@code getURL} method. |
168 * <p> |
168 * <p> |
169 * The default value of this variable is the value of the URL |
169 * The default value of this variable is the value of the URL |
170 * argument in the <code>URLConnection</code> constructor. |
170 * argument in the {@code URLConnection} constructor. |
171 * |
171 * |
172 * @see java.net.URLConnection#getURL() |
172 * @see java.net.URLConnection#getURL() |
173 * @see java.net.URLConnection#url |
173 * @see java.net.URLConnection#url |
174 */ |
174 */ |
175 protected URL url; |
175 protected URL url; |
176 |
176 |
177 /** |
177 /** |
178 * This variable is set by the <code>setDoInput</code> method. Its |
178 * This variable is set by the {@code setDoInput} method. Its |
179 * value is returned by the <code>getDoInput</code> method. |
179 * value is returned by the {@code getDoInput} method. |
180 * <p> |
180 * <p> |
181 * A URL connection can be used for input and/or output. Setting the |
181 * A URL connection can be used for input and/or output. Setting the |
182 * <code>doInput</code> flag to <code>true</code> indicates that |
182 * {@code doInput} flag to {@code true} indicates that |
183 * the application intends to read data from the URL connection. |
183 * the application intends to read data from the URL connection. |
184 * <p> |
184 * <p> |
185 * The default value of this field is <code>true</code>. |
185 * The default value of this field is {@code true}. |
186 * |
186 * |
187 * @see java.net.URLConnection#getDoInput() |
187 * @see java.net.URLConnection#getDoInput() |
188 * @see java.net.URLConnection#setDoInput(boolean) |
188 * @see java.net.URLConnection#setDoInput(boolean) |
189 */ |
189 */ |
190 protected boolean doInput = true; |
190 protected boolean doInput = true; |
191 |
191 |
192 /** |
192 /** |
193 * This variable is set by the <code>setDoOutput</code> method. Its |
193 * This variable is set by the {@code setDoOutput} method. Its |
194 * value is returned by the <code>getDoOutput</code> method. |
194 * value is returned by the {@code getDoOutput} method. |
195 * <p> |
195 * <p> |
196 * A URL connection can be used for input and/or output. Setting the |
196 * A URL connection can be used for input and/or output. Setting the |
197 * <code>doOutput</code> flag to <code>true</code> indicates |
197 * {@code doOutput} flag to {@code true} indicates |
198 * that the application intends to write data to the URL connection. |
198 * that the application intends to write data to the URL connection. |
199 * <p> |
199 * <p> |
200 * The default value of this field is <code>false</code>. |
200 * The default value of this field is {@code false}. |
201 * |
201 * |
202 * @see java.net.URLConnection#getDoOutput() |
202 * @see java.net.URLConnection#getDoOutput() |
203 * @see java.net.URLConnection#setDoOutput(boolean) |
203 * @see java.net.URLConnection#setDoOutput(boolean) |
204 */ |
204 */ |
205 protected boolean doOutput = false; |
205 protected boolean doOutput = false; |
206 |
206 |
207 private static boolean defaultAllowUserInteraction = false; |
207 private static boolean defaultAllowUserInteraction = false; |
208 |
208 |
209 /** |
209 /** |
210 * If <code>true</code>, this <code>URL</code> is being examined in |
210 * If {@code true}, this {@code URL} is being examined in |
211 * a context in which it makes sense to allow user interactions such |
211 * a context in which it makes sense to allow user interactions such |
212 * as popping up an authentication dialog. If <code>false</code>, |
212 * as popping up an authentication dialog. If {@code false}, |
213 * then no user interaction is allowed. |
213 * then no user interaction is allowed. |
214 * <p> |
214 * <p> |
215 * The value of this field can be set by the |
215 * The value of this field can be set by the |
216 * <code>setAllowUserInteraction</code> method. |
216 * {@code setAllowUserInteraction} method. |
217 * Its value is returned by the |
217 * Its value is returned by the |
218 * <code>getAllowUserInteraction</code> method. |
218 * {@code getAllowUserInteraction} method. |
219 * Its default value is the value of the argument in the last invocation |
219 * Its default value is the value of the argument in the last invocation |
220 * of the <code>setDefaultAllowUserInteraction</code> method. |
220 * of the {@code setDefaultAllowUserInteraction} method. |
221 * |
221 * |
222 * @see java.net.URLConnection#getAllowUserInteraction() |
222 * @see java.net.URLConnection#getAllowUserInteraction() |
223 * @see java.net.URLConnection#setAllowUserInteraction(boolean) |
223 * @see java.net.URLConnection#setAllowUserInteraction(boolean) |
224 * @see java.net.URLConnection#setDefaultAllowUserInteraction(boolean) |
224 * @see java.net.URLConnection#setDefaultAllowUserInteraction(boolean) |
225 */ |
225 */ |
226 protected boolean allowUserInteraction = defaultAllowUserInteraction; |
226 protected boolean allowUserInteraction = defaultAllowUserInteraction; |
227 |
227 |
228 private static boolean defaultUseCaches = true; |
228 private static boolean defaultUseCaches = true; |
229 |
229 |
230 /** |
230 /** |
231 * If <code>true</code>, the protocol is allowed to use caching |
231 * If {@code true}, the protocol is allowed to use caching |
232 * whenever it can. If <code>false</code>, the protocol must always |
232 * whenever it can. If {@code false}, the protocol must always |
233 * try to get a fresh copy of the object. |
233 * try to get a fresh copy of the object. |
234 * <p> |
234 * <p> |
235 * This field is set by the <code>setUseCaches</code> method. Its |
235 * This field is set by the {@code setUseCaches} method. Its |
236 * value is returned by the <code>getUseCaches</code> method. |
236 * value is returned by the {@code getUseCaches} method. |
237 * <p> |
237 * <p> |
238 * Its default value is the value given in the last invocation of the |
238 * Its default value is the value given in the last invocation of the |
239 * <code>setDefaultUseCaches</code> method. |
239 * {@code setDefaultUseCaches} method. |
240 * |
240 * |
241 * @see java.net.URLConnection#setUseCaches(boolean) |
241 * @see java.net.URLConnection#setUseCaches(boolean) |
242 * @see java.net.URLConnection#getUseCaches() |
242 * @see java.net.URLConnection#getUseCaches() |
243 * @see java.net.URLConnection#setDefaultUseCaches(boolean) |
243 * @see java.net.URLConnection#setDefaultUseCaches(boolean) |
244 */ |
244 */ |
250 * <p> |
250 * <p> |
251 * A nonzero value gives a time as the number of milliseconds since |
251 * A nonzero value gives a time as the number of milliseconds since |
252 * January 1, 1970, GMT. The object is fetched only if it has been |
252 * January 1, 1970, GMT. The object is fetched only if it has been |
253 * modified more recently than that time. |
253 * modified more recently than that time. |
254 * <p> |
254 * <p> |
255 * This variable is set by the <code>setIfModifiedSince</code> |
255 * This variable is set by the {@code setIfModifiedSince} |
256 * method. Its value is returned by the |
256 * method. Its value is returned by the |
257 * <code>getIfModifiedSince</code> method. |
257 * {@code getIfModifiedSince} method. |
258 * <p> |
258 * <p> |
259 * The default value of this field is <code>0</code>, indicating |
259 * The default value of this field is {@code 0}, indicating |
260 * that the fetching must always occur. |
260 * that the fetching must always occur. |
261 * |
261 * |
262 * @see java.net.URLConnection#getIfModifiedSince() |
262 * @see java.net.URLConnection#getIfModifiedSince() |
263 * @see java.net.URLConnection#setIfModifiedSince(long) |
263 * @see java.net.URLConnection#setIfModifiedSince(long) |
264 */ |
264 */ |
265 protected long ifModifiedSince = 0; |
265 protected long ifModifiedSince = 0; |
266 |
266 |
267 /** |
267 /** |
268 * If <code>false</code>, this connection object has not created a |
268 * If {@code false}, this connection object has not created a |
269 * communications link to the specified URL. If <code>true</code>, |
269 * communications link to the specified URL. If {@code true}, |
270 * the communications link has been established. |
270 * the communications link has been established. |
271 */ |
271 */ |
272 protected boolean connected = false; |
272 protected boolean connected = false; |
273 |
273 |
274 /** |
274 /** |
487 return -1; |
487 return -1; |
488 return (int) l; |
488 return (int) l; |
489 } |
489 } |
490 |
490 |
491 /** |
491 /** |
492 * Returns the value of the <code>content-length</code> header field as a |
492 * Returns the value of the {@code content-length} header field as a |
493 * long. |
493 * long. |
494 * |
494 * |
495 * @return the content length of the resource that this connection's URL |
495 * @return the content length of the resource that this connection's URL |
496 * references, or <code>-1</code> if the content length is |
496 * references, or {@code -1} if the content length is |
497 * not known. |
497 * not known. |
498 * @since 7.0 |
498 * @since 7.0 |
499 */ |
499 */ |
500 public long getContentLengthLong() { |
500 public long getContentLengthLong() { |
501 return getHeaderFieldLong("content-length", -1); |
501 return getHeaderFieldLong("content-length", -1); |
502 } |
502 } |
503 |
503 |
504 /** |
504 /** |
505 * Returns the value of the <code>content-type</code> header field. |
505 * Returns the value of the {@code content-type} header field. |
506 * |
506 * |
507 * @return the content type of the resource that the URL references, |
507 * @return the content type of the resource that the URL references, |
508 * or <code>null</code> if not known. |
508 * or {@code null} if not known. |
509 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
509 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
510 */ |
510 */ |
511 public String getContentType() { |
511 public String getContentType() { |
512 return getHeaderField("content-type"); |
512 return getHeaderField("content-type"); |
513 } |
513 } |
514 |
514 |
515 /** |
515 /** |
516 * Returns the value of the <code>content-encoding</code> header field. |
516 * Returns the value of the {@code content-encoding} header field. |
517 * |
517 * |
518 * @return the content encoding of the resource that the URL references, |
518 * @return the content encoding of the resource that the URL references, |
519 * or <code>null</code> if not known. |
519 * or {@code null} if not known. |
520 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
520 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
521 */ |
521 */ |
522 public String getContentEncoding() { |
522 public String getContentEncoding() { |
523 return getHeaderField("content-encoding"); |
523 return getHeaderField("content-encoding"); |
524 } |
524 } |
525 |
525 |
526 /** |
526 /** |
527 * Returns the value of the <code>expires</code> header field. |
527 * Returns the value of the {@code expires} header field. |
528 * |
528 * |
529 * @return the expiration date of the resource that this URL references, |
529 * @return the expiration date of the resource that this URL references, |
530 * or 0 if not known. The value is the number of milliseconds since |
530 * or 0 if not known. The value is the number of milliseconds since |
531 * January 1, 1970 GMT. |
531 * January 1, 1970 GMT. |
532 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
532 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
534 public long getExpiration() { |
534 public long getExpiration() { |
535 return getHeaderFieldDate("expires", 0); |
535 return getHeaderFieldDate("expires", 0); |
536 } |
536 } |
537 |
537 |
538 /** |
538 /** |
539 * Returns the value of the <code>date</code> header field. |
539 * Returns the value of the {@code date} header field. |
540 * |
540 * |
541 * @return the sending date of the resource that the URL references, |
541 * @return the sending date of the resource that the URL references, |
542 * or <code>0</code> if not known. The value returned is the |
542 * or {@code 0} if not known. The value returned is the |
543 * number of milliseconds since January 1, 1970 GMT. |
543 * number of milliseconds since January 1, 1970 GMT. |
544 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
544 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
545 */ |
545 */ |
546 public long getDate() { |
546 public long getDate() { |
547 return getHeaderFieldDate("date", 0); |
547 return getHeaderFieldDate("date", 0); |
548 } |
548 } |
549 |
549 |
550 /** |
550 /** |
551 * Returns the value of the <code>last-modified</code> header field. |
551 * Returns the value of the {@code last-modified} header field. |
552 * The result is the number of milliseconds since January 1, 1970 GMT. |
552 * The result is the number of milliseconds since January 1, 1970 GMT. |
553 * |
553 * |
554 * @return the date the resource referenced by this |
554 * @return the date the resource referenced by this |
555 * <code>URLConnection</code> was last modified, or 0 if not known. |
555 * {@code URLConnection} was last modified, or 0 if not known. |
556 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
556 * @see java.net.URLConnection#getHeaderField(java.lang.String) |
557 */ |
557 */ |
558 public long getLastModified() { |
558 public long getLastModified() { |
559 return getHeaderFieldDate("last-modified", 0); |
559 return getHeaderFieldDate("last-modified", 0); |
560 } |
560 } |
636 /** |
636 /** |
637 * Returns the value of the named field parsed as date. |
637 * Returns the value of the named field parsed as date. |
638 * The result is the number of milliseconds since January 1, 1970 GMT |
638 * The result is the number of milliseconds since January 1, 1970 GMT |
639 * represented by the named field. |
639 * represented by the named field. |
640 * <p> |
640 * <p> |
641 * This form of <code>getHeaderField</code> exists because some |
641 * This form of {@code getHeaderField} exists because some |
642 * connection types (e.g., <code>http-ng</code>) have pre-parsed |
642 * connection types (e.g., {@code http-ng}) have pre-parsed |
643 * headers. Classes for that connection type can override this method |
643 * headers. Classes for that connection type can override this method |
644 * and short-circuit the parsing. |
644 * and short-circuit the parsing. |
645 * |
645 * |
646 * @param name the name of the header field. |
646 * @param name the name of the header field. |
647 * @param Default a default value. |
647 * @param Default a default value. |
648 * @return the value of the field, parsed as a date. The value of the |
648 * @return the value of the field, parsed as a date. The value of the |
649 * <code>Default</code> argument is returned if the field is |
649 * {@code Default} argument is returned if the field is |
650 * missing or malformed. |
650 * missing or malformed. |
651 */ |
651 */ |
652 @SuppressWarnings("deprecation") |
652 @SuppressWarnings("deprecation") |
653 public long getHeaderFieldDate(String name, long Default) { |
653 public long getHeaderFieldDate(String name, long Default) { |
654 String value = getHeaderField(name); |
654 String value = getHeaderField(name); |
657 } catch (Exception e) { } |
657 } catch (Exception e) { } |
658 return Default; |
658 return Default; |
659 } |
659 } |
660 |
660 |
661 /** |
661 /** |
662 * Returns the key for the <code>n</code><sup>th</sup> header field. |
662 * Returns the key for the {@code n}<sup>th</sup> header field. |
663 * It returns <code>null</code> if there are fewer than <code>n+1</code> fields. |
663 * It returns {@code null} if there are fewer than {@code n+1} fields. |
664 * |
664 * |
665 * @param n an index, where {@code n>=0} |
665 * @param n an index, where {@code n>=0} |
666 * @return the key for the <code>n</code><sup>th</sup> header field, |
666 * @return the key for the {@code n}<sup>th</sup> header field, |
667 * or <code>null</code> if there are fewer than <code>n+1</code> |
667 * or {@code null} if there are fewer than {@code n+1} |
668 * fields. |
668 * fields. |
669 */ |
669 */ |
670 public String getHeaderFieldKey(int n) { |
670 public String getHeaderFieldKey(int n) { |
671 return null; |
671 return null; |
672 } |
672 } |
673 |
673 |
674 /** |
674 /** |
675 * Returns the value for the <code>n</code><sup>th</sup> header field. |
675 * Returns the value for the {@code n}<sup>th</sup> header field. |
676 * It returns <code>null</code> if there are fewer than |
676 * It returns {@code null} if there are fewer than |
677 * <code>n+1</code>fields. |
677 * {@code n+1}fields. |
678 * <p> |
678 * <p> |
679 * This method can be used in conjunction with the |
679 * This method can be used in conjunction with the |
680 * {@link #getHeaderFieldKey(int) getHeaderFieldKey} method to iterate through all |
680 * {@link #getHeaderFieldKey(int) getHeaderFieldKey} method to iterate through all |
681 * the headers in the message. |
681 * the headers in the message. |
682 * |
682 * |
683 * @param n an index, where {@code n>=0} |
683 * @param n an index, where {@code n>=0} |
684 * @return the value of the <code>n</code><sup>th</sup> header field |
684 * @return the value of the {@code n}<sup>th</sup> header field |
685 * or <code>null</code> if there are fewer than <code>n+1</code> fields |
685 * or {@code null} if there are fewer than {@code n+1} fields |
686 * @see java.net.URLConnection#getHeaderFieldKey(int) |
686 * @see java.net.URLConnection#getHeaderFieldKey(int) |
687 */ |
687 */ |
688 public String getHeaderField(int n) { |
688 public String getHeaderField(int n) { |
689 return null; |
689 return null; |
690 } |
690 } |
691 |
691 |
692 /** |
692 /** |
693 * Retrieves the contents of this URL connection. |
693 * Retrieves the contents of this URL connection. |
694 * <p> |
694 * <p> |
695 * This method first determines the content type of the object by |
695 * This method first determines the content type of the object by |
696 * calling the <code>getContentType</code> method. If this is |
696 * calling the {@code getContentType} method. If this is |
697 * the first time that the application has seen that specific content |
697 * the first time that the application has seen that specific content |
698 * type, a content handler for that content type is created: |
698 * type, a content handler for that content type is created: |
699 * <ol> |
699 * <ol> |
700 * <li>If the application has set up a content handler factory instance |
700 * <li>If the application has set up a content handler factory instance |
701 * using the <code>setContentHandlerFactory</code> method, the |
701 * using the {@code setContentHandlerFactory} method, the |
702 * <code>createContentHandler</code> method of that instance is called |
702 * {@code createContentHandler} method of that instance is called |
703 * with the content type as an argument; the result is a content |
703 * with the content type as an argument; the result is a content |
704 * handler for that content type. |
704 * handler for that content type. |
705 * <li>If no content handler factory has yet been set up, or if the |
705 * <li>If no content handler factory has yet been set up, or if the |
706 * factory's <code>createContentHandler</code> method returns |
706 * factory's {@code createContentHandler} method returns |
707 * <code>null</code>, then the application loads the class named: |
707 * {@code null}, then the application loads the class named: |
708 * <blockquote><pre> |
708 * <blockquote><pre> |
709 * sun.net.www.content.<<i>contentType</i>> |
709 * sun.net.www.content.<<i>contentType</i>> |
710 * </pre></blockquote> |
710 * </pre></blockquote> |
711 * where <<i>contentType</i>> is formed by taking the |
711 * where <<i>contentType</i>> is formed by taking the |
712 * content-type string, replacing all slash characters with a |
712 * content-type string, replacing all slash characters with a |
713 * <code>period</code> ('.'), and all other non-alphanumeric characters |
713 * {@code period} ('.'), and all other non-alphanumeric characters |
714 * with the underscore character '<code>_</code>'. The alphanumeric |
714 * with the underscore character '{@code _}'. The alphanumeric |
715 * characters are specifically the 26 uppercase ASCII letters |
715 * characters are specifically the 26 uppercase ASCII letters |
716 * '<code>A</code>' through '<code>Z</code>', the 26 lowercase ASCII |
716 * '{@code A}' through '{@code Z}', the 26 lowercase ASCII |
717 * letters '<code>a</code>' through '<code>z</code>', and the 10 ASCII |
717 * letters '{@code a}' through '{@code z}', and the 10 ASCII |
718 * digits '<code>0</code>' through '<code>9</code>'. If the specified |
718 * digits '{@code 0}' through '{@code 9}'. If the specified |
719 * class does not exist, or is not a subclass of |
719 * class does not exist, or is not a subclass of |
720 * <code>ContentHandler</code>, then an |
720 * {@code ContentHandler}, then an |
721 * <code>UnknownServiceException</code> is thrown. |
721 * {@code UnknownServiceException} is thrown. |
722 * </ol> |
722 * </ol> |
723 * |
723 * |
724 * @return the object fetched. The <code>instanceof</code> operator |
724 * @return the object fetched. The {@code instanceof} operator |
725 * should be used to determine the specific kind of object |
725 * should be used to determine the specific kind of object |
726 * returned. |
726 * returned. |
727 * @exception IOException if an I/O error occurs while |
727 * @exception IOException if an I/O error occurs while |
728 * getting the content. |
728 * getting the content. |
729 * @exception UnknownServiceException if the protocol does not support |
729 * @exception UnknownServiceException if the protocol does not support |
771 /** |
771 /** |
772 * Returns a permission object representing the permission |
772 * Returns a permission object representing the permission |
773 * necessary to make the connection represented by this |
773 * necessary to make the connection represented by this |
774 * object. This method returns null if no permission is |
774 * object. This method returns null if no permission is |
775 * required to make the connection. By default, this method |
775 * required to make the connection. By default, this method |
776 * returns <code>java.security.AllPermission</code>. Subclasses |
776 * returns {@code java.security.AllPermission}. Subclasses |
777 * should override this method and return the permission |
777 * should override this method and return the permission |
778 * that best represents the permission required to make a |
778 * that best represents the permission required to make a |
779 * a connection to the URL. For example, a <code>URLConnection</code> |
779 * a connection to the URL. For example, a {@code URLConnection} |
780 * representing a <code>file:</code> URL would return a |
780 * representing a {@code file:} URL would return a |
781 * <code>java.io.FilePermission</code> object. |
781 * {@code java.io.FilePermission} object. |
782 * |
782 * |
783 * <p>The permission returned may dependent upon the state of the |
783 * <p>The permission returned may dependent upon the state of the |
784 * connection. For example, the permission before connecting may be |
784 * connection. For example, the permission before connecting may be |
785 * different from that after connecting. For example, an HTTP |
785 * different from that after connecting. For example, an HTTP |
786 * sever, say foo.com, may redirect the connection to a different |
786 * sever, say foo.com, may redirect the connection to a different |
926 throw new IllegalStateException("Already connected"); |
926 throw new IllegalStateException("Already connected"); |
927 allowUserInteraction = allowuserinteraction; |
927 allowUserInteraction = allowuserinteraction; |
928 } |
928 } |
929 |
929 |
930 /** |
930 /** |
931 * Returns the value of the <code>allowUserInteraction</code> field for |
931 * Returns the value of the {@code allowUserInteraction} field for |
932 * this object. |
932 * this object. |
933 * |
933 * |
934 * @return the value of the <code>allowUserInteraction</code> field for |
934 * @return the value of the {@code allowUserInteraction} field for |
935 * this object. |
935 * this object. |
936 * @see #setAllowUserInteraction(boolean) |
936 * @see #setAllowUserInteraction(boolean) |
937 */ |
937 */ |
938 public boolean getAllowUserInteraction() { |
938 public boolean getAllowUserInteraction() { |
939 return allowUserInteraction; |
939 return allowUserInteraction; |
940 } |
940 } |
941 |
941 |
942 /** |
942 /** |
943 * Sets the default value of the |
943 * Sets the default value of the |
944 * <code>allowUserInteraction</code> field for all future |
944 * {@code allowUserInteraction} field for all future |
945 * <code>URLConnection</code> objects to the specified value. |
945 * {@code URLConnection} objects to the specified value. |
946 * |
946 * |
947 * @param defaultallowuserinteraction the new value. |
947 * @param defaultallowuserinteraction the new value. |
948 * @see #getDefaultAllowUserInteraction() |
948 * @see #getDefaultAllowUserInteraction() |
949 */ |
949 */ |
950 public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) { |
950 public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) { |
951 defaultAllowUserInteraction = defaultallowuserinteraction; |
951 defaultAllowUserInteraction = defaultallowuserinteraction; |
952 } |
952 } |
953 |
953 |
954 /** |
954 /** |
955 * Returns the default value of the <code>allowUserInteraction</code> |
955 * Returns the default value of the {@code allowUserInteraction} |
956 * field. |
956 * field. |
957 * <p> |
957 * <p> |
958 * Ths default is "sticky", being a part of the static state of all |
958 * Ths default is "sticky", being a part of the static state of all |
959 * URLConnections. This flag applies to the next, and all following |
959 * URLConnections. This flag applies to the next, and all following |
960 * URLConnections that are created. |
960 * URLConnections that are created. |
961 * |
961 * |
962 * @return the default value of the <code>allowUserInteraction</code> |
962 * @return the default value of the {@code allowUserInteraction} |
963 * field. |
963 * field. |
964 * @see #setDefaultAllowUserInteraction(boolean) |
964 * @see #setDefaultAllowUserInteraction(boolean) |
965 */ |
965 */ |
966 public static boolean getDefaultAllowUserInteraction() { |
966 public static boolean getDefaultAllowUserInteraction() { |
967 return defaultAllowUserInteraction; |
967 return defaultAllowUserInteraction; |
968 } |
968 } |
969 |
969 |
970 /** |
970 /** |
971 * Sets the value of the <code>useCaches</code> field of this |
971 * Sets the value of the {@code useCaches} field of this |
972 * <code>URLConnection</code> to the specified value. |
972 * {@code URLConnection} to the specified value. |
973 * <p> |
973 * <p> |
974 * Some protocols do caching of documents. Occasionally, it is important |
974 * Some protocols do caching of documents. Occasionally, it is important |
975 * to be able to "tunnel through" and ignore the caches (e.g., the |
975 * to be able to "tunnel through" and ignore the caches (e.g., the |
976 * "reload" button in a browser). If the UseCaches flag on a connection |
976 * "reload" button in a browser). If the UseCaches flag on a connection |
977 * is true, the connection is allowed to use whatever caches it can. |
977 * is true, the connection is allowed to use whatever caches it can. |
978 * If false, caches are to be ignored. |
978 * If false, caches are to be ignored. |
979 * The default value comes from DefaultUseCaches, which defaults to |
979 * The default value comes from DefaultUseCaches, which defaults to |
980 * true. |
980 * true. |
981 * |
981 * |
982 * @param usecaches a <code>boolean</code> indicating whether |
982 * @param usecaches a {@code boolean} indicating whether |
983 * or not to allow caching |
983 * or not to allow caching |
984 * @throws IllegalStateException if already connected |
984 * @throws IllegalStateException if already connected |
985 * @see #getUseCaches() |
985 * @see #getUseCaches() |
986 */ |
986 */ |
987 public void setUseCaches(boolean usecaches) { |
987 public void setUseCaches(boolean usecaches) { |
1015 throw new IllegalStateException("Already connected"); |
1015 throw new IllegalStateException("Already connected"); |
1016 ifModifiedSince = ifmodifiedsince; |
1016 ifModifiedSince = ifmodifiedsince; |
1017 } |
1017 } |
1018 |
1018 |
1019 /** |
1019 /** |
1020 * Returns the value of this object's <code>ifModifiedSince</code> field. |
1020 * Returns the value of this object's {@code ifModifiedSince} field. |
1021 * |
1021 * |
1022 * @return the value of this object's <code>ifModifiedSince</code> field. |
1022 * @return the value of this object's {@code ifModifiedSince} field. |
1023 * @see #setIfModifiedSince(long) |
1023 * @see #setIfModifiedSince(long) |
1024 */ |
1024 */ |
1025 public long getIfModifiedSince() { |
1025 public long getIfModifiedSince() { |
1026 return ifModifiedSince; |
1026 return ifModifiedSince; |
1027 } |
1027 } |
1028 |
1028 |
1029 /** |
1029 /** |
1030 * Returns the default value of a <code>URLConnection</code>'s |
1030 * Returns the default value of a {@code URLConnection}'s |
1031 * <code>useCaches</code> flag. |
1031 * {@code useCaches} flag. |
1032 * <p> |
1032 * <p> |
1033 * Ths default is "sticky", being a part of the static state of all |
1033 * Ths default is "sticky", being a part of the static state of all |
1034 * URLConnections. This flag applies to the next, and all following |
1034 * URLConnections. This flag applies to the next, and all following |
1035 * URLConnections that are created. |
1035 * URLConnections that are created. |
1036 * |
1036 * |
1037 * @return the default value of a <code>URLConnection</code>'s |
1037 * @return the default value of a {@code URLConnection}'s |
1038 * <code>useCaches</code> flag. |
1038 * {@code useCaches} flag. |
1039 * @see #setDefaultUseCaches(boolean) |
1039 * @see #setDefaultUseCaches(boolean) |
1040 */ |
1040 */ |
1041 public boolean getDefaultUseCaches() { |
1041 public boolean getDefaultUseCaches() { |
1042 return defaultUseCaches; |
1042 return defaultUseCaches; |
1043 } |
1043 } |
1044 |
1044 |
1045 /** |
1045 /** |
1046 * Sets the default value of the <code>useCaches</code> field to the |
1046 * Sets the default value of the {@code useCaches} field to the |
1047 * specified value. |
1047 * specified value. |
1048 * |
1048 * |
1049 * @param defaultusecaches the new value. |
1049 * @param defaultusecaches the new value. |
1050 * @see #getDefaultUseCaches() |
1050 * @see #getDefaultUseCaches() |
1051 */ |
1051 */ |
1195 * The ContentHandler factory. |
1195 * The ContentHandler factory. |
1196 */ |
1196 */ |
1197 static ContentHandlerFactory factory; |
1197 static ContentHandlerFactory factory; |
1198 |
1198 |
1199 /** |
1199 /** |
1200 * Sets the <code>ContentHandlerFactory</code> of an |
1200 * Sets the {@code ContentHandlerFactory} of an |
1201 * application. It can be called at most once by an application. |
1201 * application. It can be called at most once by an application. |
1202 * <p> |
1202 * <p> |
1203 * The <code>ContentHandlerFactory</code> instance is used to |
1203 * The {@code ContentHandlerFactory} instance is used to |
1204 * construct a content handler from a content type |
1204 * construct a content handler from a content type |
1205 * <p> |
1205 * <p> |
1206 * If there is a security manager, this method first calls |
1206 * If there is a security manager, this method first calls |
1207 * the security manager's <code>checkSetFactory</code> method |
1207 * the security manager's {@code checkSetFactory} method |
1208 * to ensure the operation is allowed. |
1208 * to ensure the operation is allowed. |
1209 * This could result in a SecurityException. |
1209 * This could result in a SecurityException. |
1210 * |
1210 * |
1211 * @param fac the desired factory. |
1211 * @param fac the desired factory. |
1212 * @exception Error if the factory has already been defined. |
1212 * @exception Error if the factory has already been defined. |
1213 * @exception SecurityException if a security manager exists and its |
1213 * @exception SecurityException if a security manager exists and its |
1214 * <code>checkSetFactory</code> method doesn't allow the operation. |
1214 * {@code checkSetFactory} method doesn't allow the operation. |
1215 * @see java.net.ContentHandlerFactory |
1215 * @see java.net.ContentHandlerFactory |
1216 * @see java.net.URLConnection#getContent() |
1216 * @see java.net.URLConnection#getContent() |
1217 * @see SecurityManager#checkSetFactory |
1217 * @see SecurityManager#checkSetFactory |
1218 */ |
1218 */ |
1219 public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) { |
1219 public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) { |
1387 |
1387 |
1388 /** |
1388 /** |
1389 * Tries to determine the type of an input stream based on the |
1389 * Tries to determine the type of an input stream based on the |
1390 * characters at the beginning of the input stream. This method can |
1390 * characters at the beginning of the input stream. This method can |
1391 * be used by subclasses that override the |
1391 * be used by subclasses that override the |
1392 * <code>getContentType</code> method. |
1392 * {@code getContentType} method. |
1393 * <p> |
1393 * <p> |
1394 * Ideally, this routine would not be needed. But many |
1394 * Ideally, this routine would not be needed. But many |
1395 * <code>http</code> servers return the incorrect content type; in |
1395 * {@code http} servers return the incorrect content type; in |
1396 * addition, there are many nonstandard extensions. Direct inspection |
1396 * addition, there are many nonstandard extensions. Direct inspection |
1397 * of the bytes to determine the content type is often more accurate |
1397 * of the bytes to determine the content type is often more accurate |
1398 * than believing the content type claimed by the <code>http</code> server. |
1398 * than believing the content type claimed by the {@code http} server. |
1399 * |
1399 * |
1400 * @param is an input stream that supports marks. |
1400 * @param is an input stream that supports marks. |
1401 * @return a guess at the content type, or <code>null</code> if none |
1401 * @return a guess at the content type, or {@code null} if none |
1402 * can be determined. |
1402 * can be determined. |
1403 * @exception IOException if an I/O error occurs while reading the |
1403 * @exception IOException if an I/O error occurs while reading the |
1404 * input stream. |
1404 * input stream. |
1405 * @see java.io.InputStream#mark(int) |
1405 * @see java.io.InputStream#mark(int) |
1406 * @see java.io.InputStream#markSupported() |
1406 * @see java.io.InputStream#markSupported() |