APIdia Launch Partner
Top Description Fields Constructors Methods
sun.net.www

public abstract Class URLConnection

extends URLConnection
Class Inheritance
Known Direct Subclasses
sun.net.www.protocol.file.FileURLConnection, sun.net.www.protocol.ftp.FtpURLConnection, sun.net.www.protocol.jrt.JavaRuntimeURLConnection, sun.net.www.protocol.mailto.MailToURLConnection
Imports
java.io.IOException, java.net.URL, java.util.*

A class to represent an active connection to an object represented by a URL.
Author
James Gosling

Field Summary

Modifier and TypeField and Description
private int
private String
contentType

The URL that it is connected to

private Map<String, List<String>>
protected MessageHeader
private static final HashMap<String, Void>
Inherited from java.net.URLConnection:
allowUserInteractionconnecteddoInputdoOutputifModifiedSinceurluseCaches

Constructor Summary

AccessConstructor and Description
public
URLConnection(URL
The URL that this connects to.
u
)

Create a URLConnection object.

Method Summary

Modifier and TypeMethod and Description
public void
addRequestProperty(String key, String value)

Overrides java.net.URLConnection.addRequestProperty.

The following three methods addRequestProperty, getRequestProperty, and getRequestProperties were copied from the superclass implementation before it was changed by CR:6230836, to maintain backward compatibility.

public boolean
canCache()

Returns true if the data associated with this URL can be cached.

public void
close()

Call this to close the connection and flush any remaining data.

protected void
ensureCanServeHeaders()

This method is called whenever the headers related methods are called on the URLConnection.

public int
getContentLength()

Overrides java.net.URLConnection.getContentLength.

Call this routine to get the content-length associated with this object.

public String
getContentType()

Overrides java.net.URLConnection.getContentType.

Call this routine to get the content-type associated with this object.

public String
getHeaderField(String name)

Overrides java.net.URLConnection.getHeaderField.

Returns the value of the named header field.

public String
getHeaderField(int n)

Overrides java.net.URLConnection.getHeaderField.

Return the value for the nth header field.

public String
getHeaderFieldKey(int n)

Overrides java.net.URLConnection.getHeaderFieldKey.

Return the key for the nth header field.

public Map<String, List<String>>
getHeaderFields()

Overrides java.net.URLConnection.getHeaderFields.

Returns an unmodifiable Map of the header fields.

public MessageHeader
getProperties()

Call this routine to get the property list for this object.

public Map<String, List<String>>
getRequestProperties()

Overrides java.net.URLConnection.getRequestProperties.

Returns an unmodifiable Map of general request properties for this connection.

public String
getRequestProperty(String key)

Overrides java.net.URLConnection.getRequestProperty.

Returns the value of the named general request property for this connection.

public static synchronized boolean
protected void
setContentLength(int length)

Call this routine to set the content-length associated with this object.

public void
setContentType(String
The content type to use. One of the content_* static variables in this class should be used. e.g. setType(URL.content_html);
type
)

Set the content type of this URL to a specific value.

public void
setProperties(MessageHeader properties)

Call this routine to set the property list for this object.

public static synchronized void
public void
setRequestProperty(String key, String value)

Overrides java.net.URLConnection.setRequestProperty.

Sets the general request property.

Inherited from java.net.URLConnection:
connectgetAllowUserInteractiongetConnectTimeoutgetContentgetContentgetContentEncodinggetContentLengthLonggetDategetDefaultAllowUserInteractiongetDefaultRequestPropertygetDefaultUseCachesgetDefaultUseCachesgetDoInputgetDoOutputgetExpirationgetFileNameMapgetHeaderFieldDategetHeaderFieldIntgetHeaderFieldLonggetIfModifiedSincegetInputStreamgetLastModifiedgetOutputStreamgetPermissiongetReadTimeoutgetURLgetUseCachesguessContentTypeFromNameguessContentTypeFromStreamsetAllowUserInteractionsetConnectTimeoutsetContentHandlerFactorysetDefaultAllowUserInteractionsetDefaultRequestPropertysetDefaultUseCachessetDefaultUseCachessetDoInputsetDoOutputsetFileNameMapsetIfModifiedSincesetReadTimeoutsetUseCachestoString

Field Detail

contentLengthback to summary
private int contentLength
contentTypeback to summary
private String contentType

The URL that it is connected to

headerFieldsback to summary
private Map<String, List<String>> headerFields
propertiesback to summary
protected MessageHeader properties
proxiedHostsback to summary
private static final HashMap<String, Void> proxiedHosts

Constructor Detail

URLConnectionback to summary
public URLConnection(URL u)

Create a URLConnection object. These should not be created directly: instead they should be created by protocol handlers in response to URL.openConnection.

Parameters
u:URL

The URL that this connects to.

Method Detail

addRequestPropertyback to summary
public void addRequestProperty(String key, String value)

Overrides java.net.URLConnection.addRequestProperty.

The following three methods addRequestProperty, getRequestProperty, and getRequestProperties were copied from the superclass implementation before it was changed by CR:6230836, to maintain backward compatibility.

canCacheback to summary
public boolean canCache()

Returns true if the data associated with this URL can be cached.

closeback to summary
public void close()

Call this to close the connection and flush any remaining data. Overriders must remember to call super.close()

ensureCanServeHeadersback to summary
protected void ensureCanServeHeaders() throws IOException

This method is called whenever the headers related methods are called on the URLConnection. This method does any necessary checks and initializations to make sure that the headers can be served. If this URLConnection cannot serve the headers, then this method throws an IOException.

Exceptions
IOException:
if the headers cannot be served
getContentLengthback to summary
public int getContentLength()

Overrides java.net.URLConnection.getContentLength.

Call this routine to get the content-length associated with this object.

getContentTypeback to summary
public String getContentType()

Overrides java.net.URLConnection.getContentType.

Call this routine to get the content-type associated with this object.

getHeaderFieldback to summary
public String getHeaderField(String name)

Overrides java.net.URLConnection.getHeaderField.

Doc from java.net.URLConnection.getHeaderField.

Returns the value of the named header field.

If called on a connection that sets the same header multiple times with possibly different values, only the last value is returned.

getHeaderFieldback to summary
public String getHeaderField(int n)

Overrides java.net.URLConnection.getHeaderField.

Return the value for the nth header field. Returns null if there are fewer than n fields. This can be used in conjunction with getHeaderFieldKey to iterate through all the headers in the message.

getHeaderFieldKeyback to summary
public String getHeaderFieldKey(int n)

Overrides java.net.URLConnection.getHeaderFieldKey.

Return the key for the nth header field. Returns null if there are fewer than n fields. This can be used to iterate through all the headers in the message.

getHeaderFieldsback to summary
public Map<String, List<String>> getHeaderFields()

Overrides java.net.URLConnection.getHeaderFields.

Doc from java.net.URLConnection.getHeaderFields.

Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values. This method is overridden by the subclasses of URLConnection. In the implementation of these methods, if a given key has multiple corresponding values, they must be returned in the order they were added, preserving the insertion-order.

Annotations
@Override
getPropertiesback to summary
public MessageHeader getProperties()

Call this routine to get the property list for this object. Properties (like content-type) that have explicit getXX() methods associated with them should be accessed using those methods.

getRequestPropertiesback to summary
public Map<String, List<String>> getRequestProperties()

Overrides java.net.URLConnection.getRequestProperties.

Doc from java.net.URLConnection.getRequestProperties.

Returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values. If multiple values for a given key are added via the addRequestProperty(String, String) method, these values will be returned in the order they were added. This method must preserve the insertion order of such values. The default implementation of this method preserves the insertion order when multiple values are added for a given key. The values are returned in the order they were added.

getRequestPropertyback to summary
public String getRequestProperty(String key)

Overrides java.net.URLConnection.getRequestProperty.

Doc from java.net.URLConnection.getRequestProperty.

Returns the value of the named general request property for this connection.

isProxiedHostback to summary
public static synchronized boolean isProxiedHost(String host)
setContentLengthback to summary
protected void setContentLength(int length)

Call this routine to set the content-length associated with this object.

setContentTypeback to summary
public void setContentType(String type)

Set the content type of this URL to a specific value.

Parameters
type:String

The content type to use. One of the content_* static variables in this class should be used. e.g. setType(URL.content_html);

setPropertiesback to summary
public void setProperties(MessageHeader properties)

Call this routine to set the property list for this object.

setProxiedHostback to summary
public static synchronized void setProxiedHost(String host)
setRequestPropertyback to summary
public void setRequestProperty(String key, String value)

Overrides java.net.URLConnection.setRequestProperty.

Doc from java.net.URLConnection.setRequestProperty.

Sets the general request property. If a property with the key already exists, overwrite its value with the new value.

Note

HTTP requires all request properties which can legally have multiple instances with the same key to use a comma-separated list syntax which enables multiple properties to be appended into a single property.