URLConnection

Author Avatar
罗炜光 5月 09, 2016
  • 在其它设备中阅读本文章

基本步骤

使用URLconnection类的程序遵循以下基本步骤

  1. 构造一个URL对象
  2. 调用这个URL对象的openConnection()获取一个对应URL的URLConnected对象
  3. 配置这个URLConnected
  4. 读取首部字段
  5. 获得输入流并读取数据
  6. 获得输出流并写入数据
  7. 关闭连接

配置连接

protected URL url;
protected boolean doInput = ture;
protected boolean doOutput = false;
protected boolean allowUserInteraction = defaultAllowUserInteraction;
protected boolean useCaches = defaultUseCaches;
protected long ifModifiedSince = 0;
protected boolean connected = false;

  • protected URL url
    url字段指定了这个URLConnection连接到URL。构造行数会在创建URLConnection时设置这个字段,此后不能再改变
    使用getURL() URL查询
  • protected boolean connected
    如果连接已经打开,boolean字段connected为ture,如果连接关闭,则为false。没有直接读取或改变connected值的方法,不过任何导致URLConnection连接的方法都会将这个变量设置为ture,任何导致断开的方法都会导致设置为false
  • protected boolean allowUserInteraction
    allowUserInteraction字段指示了是否允许用户交互,默认值为false。值为ture时表示允许用户进行交互,这个值只能在URLConnected连接前设置
    使用setAllowUserInteraction(boolean allowuserinteraction) void修改
    使用getAllowUserInteraction() boolean查询
  • protected boolean doInput
    URLConnection可以读取服务器,写入服务器,或者同时用于读/写服务器。如果URLconnection可以用来读取,保护类型boolean字段doInput就为ture,否则为false。默认值为ture
    使用setDoInput(boolean doinput) void修改
    使用getDoInput() boolean查询
  • protected boolean doOutput
    如果URLconnection可以用于写入,保护类型boolean字段doOutput就为ture,否则为false。默认值为false
    使用setDoOutput(boolean dooutput) void修改
    使用getDoOutput() boolean查询
  • protected long ifModifiedSince
    客户端最后获取文档的时间,即http首部中的If-Modified-Since
    使用setIfModifiedSince(long ifmodifiedsince)修改
    使用getIfModifiedSince() long查询
  • protected boolean useCaches
    useCaches变量表示是否使用缓存,默认值为ture,表示使用缓存,false表示不使用缓存
    使用setUseCaches(boolean usecaches) void修改
    使用getUseCaches() boolean查询

    获取任意首部字段

  • public String getHeaderField(String name)
    返回指定首部字段的值,首部的名不区分大小写,也不包含结束冒号
  • public String getHeaderFieldKey(int n)
    这个方法返回第n个首部字段的字段名,请求方法本身是第0个首部,它的键为null.第一个首部即编号为1
  • public String getHeaderField(int n)
    这个方法返回第n个首部字段的值,第一个首部即编号为1
  • public long getHeaderFiledDate(String name,long default)
    这个方法首先获取由name参数指定的首部字段,然后尝试将这个字符串转换为一个long,如果无法找到请求的首部字段或无法转换为long,则返回default参数

读取首部

  • public String getContentType()
    getContentType()方法返回响应主体的MIME内容类型。它依赖于Web服务器来发送一个有效的内容类型,如果没有提供内容类型,它不会抛出异常,而是返回null
  • public int getContentLength()
    getContentLength()方法告诉你内容有多少字节,如果没有Content-length首部,getContentLength()就返回-1
    public long getContentLengthLong()//Java 7
    同上
  • public String getContentEncoding()
    getContentEncoding()方法指出内容是如何编码的,如果发送的内容没有编码,这个方法就返回null
  • public long getDate()
    文档发送时间,如果HTTP首部不包含Data字段,getDate()就返回0
  • public long getExpiration()
    文档过期时间,如果首部不包含Expiration字段,getExpiration()就返回0,这表示文档不会过期
  • public long getLastModified()
    返回文档的最后修改日期,如果HTTP首部没有包括Last-modified字段,这个方法就返回0
  • public int getHeaderFiledInt(String name,long default)
    这个方法首先获取由name参数指定的首部字段,然后尝试将这个字符串转换为一个int,如果无法找到请求的首部字段或无法转换为int,则返回default参数

读取服务器数据

  • public InputStreamgetInputStream()

向服务器写入数据

  • public OutputStream getOutputStream()

超时

控制socket等待建立连接的时间
使用setConnectTimeout(int timeout) void设置
使用getConnectTimeout() int查询

控制输入流等待数据到达的时间
使用setReadTimeout(int timeout) void设置
使用getReadTimeout() int查询

两种超时都将0看做永远不超时