Android Guides | Samples

Java.Net.URLConnection Class

A connection to a URL for reading or writing.

See Also: URLConnection

Syntax

[Android.Runtime.Register("java/net/URLConnection", DoNotGenerateAcw=true)]
public abstract class URLConnection : Object

Remarks

A connection to a URL for reading or writing. For HTTP connections, see HttpURLConnection for documentation of HTTP-specific features.

For example, to retrieve ftp://mirror.csclub.uwaterloo.ca/index.html:

java Example

URL url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html");
   URLConnection urlConnection = url.openConnection();
   InputStream in = new BufferedInputStream(urlConnection.getInputStream());
   try {
     readStream(in);
    finally {
     in.close();
   }
 }

URLConnection must be configured before it has connected to the remote resource. Instances of URLConnection are not reusable: you must use a different instance for each connection to a resource.

Timeouts

URLConnection supports two timeouts: a URLConnection.ConnectTimeout and a URLConnection.ReadTimeout. By default, operations never time out.

Built-in Protocols

  • File
    Resources from the local file system can be loaded using file: URIs. File connections can only be used for input.
  • FTP
    File Transfer Protocol () is supported, but with no public subclass. FTP connections can be used for input or output but not both. By default, FTP connections will be made using anonymous as the username and the empty string as the password. Specify alternate usernames and passwords in the URL: ftp://username:password@host/path.
  • HTTP and HTTPS
    Refer to the HttpURLConnection and HttpsURLConnection subclasses.
  • Jar
    Refer to the JarURLConnection subclass.

Registering Additional Protocols

Use URL.SetURLStreamHandlerFactory(IURLStreamHandlerFactory) to register handlers for other protocol types.

[Android Documentation]

Requirements

Namespace: Java.Net
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Java.Net.URLConnection are listed below.

See Also: Object

Protected Constructors

Creates a new URLConnection instance pointing to the resource specified by the given URL.
A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Properties

AllowUserInteractionBoolean. Returns allowUserInteraction.
ConnectTimeoutInt32. Returns the connect timeout in milliseconds.
[read-only]
ContentObject. Returns an object representing the content of the resource this URLConnection is connected to.
[read-only]
ContentEncodingString. Returns the content encoding type specified by the response header field content-encoding or null if this field is not set.
[read-only]
ContentLengthInt32. Returns the content length in bytes specified by the response header field content-length or -1 if this field is not set or cannot be represented as an int.
[read-only]
ContentLengthLongInt64.
[read-only]
ContentTypeString. Returns the MIME-type of the content specified by the response header field content-type or null if type is unknown.
[read-only]
DateInt64. Returns the timestamp when this response has been sent as a date in milliseconds since January 1, 1970 GMT or 0 if this timestamp is unknown.
static
DefaultAllowUserInteractionBoolean. Returns the default value of allowUserInteraction.
DefaultUseCachesBoolean. Returns the default setting whether this connection allows using caches.
DoInputBoolean. Returns the value of the option doInput which specifies whether this connection allows to receive data.
DoOutputBoolean. Returns the value of the option doOutput which specifies whether this connection allows to send data.
[read-only]
ExpirationInt64. Returns the timestamp when this response will be expired in milliseconds since January 1, 1970 GMT or 0 if this timestamp is unknown.
static
FileNameMapIFileNameMap. Returns the table which is used by all URLConnection instances to determine the MIME-type according to a file extension.
[read-only]
HeaderFieldsIDictionary<string, IList<string>>. Returns an unmodifiable map of the response-header fields and values.
IfModifiedSinceInt64. Returns the point of time since when the data must be modified to be transmitted.
[read-only]
InputStreamStream. Returns an InputStream for reading data from the resource pointed by this URLConnection.
[read-only]
LastModifiedInt64. Returns the value of the response header field last-modified or 0 if this value is not set.
[read-only]
OutputStreamStream. Returns an OutputStream for writing data to this URLConnection.
[read-only]
PermissionPermission. Returns a Permission object representing all needed permissions to open this connection.
ReadTimeoutInt32. Returns the read timeout in milliseconds, or 0 if reads never timeout.
[read-only]
RequestPropertiesIDictionary<string, IList<string>>. Returns an unmodifiable map of general request properties used by this connection.
[read-only]
URLURL. Returns the URL represented by this URLConnection.
UseCachesBoolean. Returns the value of the flag which specifies whether this URLConnection allows to use caches.

Protected Properties

ConnectedBoolean. Specifies whether this URLConnection is already connected to the remote resource.
[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
UrlURL. The URL which represents the remote target of this URLConnection.

Public Methods

AddRequestProperty(String, String)
Adds the given property to the request header.
abstract
Connect()
Opens a connection to the resource.
ConnectAsync() : Task
GetContent(Class[]) : Object
Returns an object representing the content of the resource this URLConnection is connected to.
static
GetDefaultRequestProperty(String) : String
Returns null.
GetHeaderField(Int32) : String
Returns the header value at the field position pos or null if the header has fewer than pos fields.
GetHeaderField(String) : String
Returns the value of the header field specified by key or null if there is no field with this name.
GetHeaderFieldDate(String, Int64) : Int64
Returns the specified header value as a date in milliseconds since January 1, 1970 GMT.
GetHeaderFieldInt(String, Int32) : Int32
Returns the specified header value as a number.
GetHeaderFieldKey(Int32) : String
Returns the name of the header field at the given position posn or null if there are fewer than posn fields.
GetHeaderFieldLong(String, Int64) : Int64
GetRequestProperty(String) : String
Returns the value of the request header property specified by {code field} or null if there is no field with this name.
static
GuessContentTypeFromName(String) : String
Determines the MIME-type of the given resource url by resolving the filename extension with the internal FileNameMap.
static
GuessContentTypeFromStream(Stream) : String
Determines the MIME-type of the resource represented by the input stream is by reading its first few characters.
static
SetContentHandlerFactory(IContentHandlerFactory)
Sets the internally used content handler factory.
static
SetDefaultRequestProperty(String, String)
Does nothing.
SetRequestProperty(String, String)
Sets the value of the specified request header field.