URLConnection类的使用和总结
URLConnection类
URLConnection类概述
The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL. In general, creating a connection to a URL is a multistep process:
抽象类URLConnection 是所有应用程序和 URL 之间的通信链接的超类。此类的实例可以读取和写入被url引用的资源。通常,创建一个连接需要如下几个步骤:
1、The connection object is created by invoking the openConnection method on a URL.
——-通过在 URL 上调用 openConnection 方法创建连接对象。
2、The setup parameters and general request properties are manipulated.
——-处理设置参数和一般请求属性。
3、The actual connection to the remote object is made, using the connect method.
——-使用 connect 方法建立到远程对象的实际连接。
4、The remote object becomes available. The header fields and the contents of the remote object can be accessed.
——-远程对象变为可用。远程对象的头字段和内容变为可访问。
The setup parameters are modified using the following methods:
——-设置参数方法如下:
setAllowUserInteraction
setDoInput
setDoOutput
setIfModifiedSince
setUseCaches
and the general request properties are modified using the method:
通常设置request 属性使用如下方法
setRequestProperty
Default values for the AllowUserInteraction and UseCaches parameters can be set using the methods
对AllowUserInteraction 和UseCaches 设置默认值, 可以使用如下方法:
setDefaultAllowUserInteraction and setDefaultUseCaches.
Each of the above set methods has a corresponding get method to retrieve the value of the parameter or general request property. The specific parameters and general request properties that are applicable are protocol specific.
上面每一个set方法都有一个相应的get方法来取参数的值或者request的属性。用的特定参数和通用请求属性使用专用的协议。
The following methods are used to access the header fields and the contents after the connection is made to the remote object:
在链接远程成功之后,用下面的方法用来访问header和content
getContent
getHeaderField
getInputStream
getOutputStream
Certain header fields are accessed frequently. The methods:
getContentEncoding
getContentLength
getContentType
getDate
getExpiration
getLastModifed