天天看点

JAVA网络编程之获取网络资源URLURLConnectionHttpURLConnection

在应用程序开发过程中,很多时候我们会用网络资源,比如接口调用等。java中也提供了相关的类去处理网络资源,而我们在处理网络资源的时候,用到的最多的就是io,所以对io还不了解的可以先看看前面的博客。

java中,处理网络资源、网络通信所需要的类都在java.net包中。

类url代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。通过url,我们可以定位网络资源。

方法名

说明

url(string spec)

根据 string 表示形式创建 url 对象

url(string protocol, string host, int port, string file)

根据指定 protocol、host、port 号和 file 创建 url 对象

url(string protocol, string host, int port, string file, urlstreamhandler handler)

根据指定的 protocol、host、port 号、file 和 handler 创建 url 对象

url(string protocol, string host, string file)

根据指定的 protocol 名称、host 名称和 file 名称创建 url

url(url context, string spec)

通过在指定的上下文中对给定的 spec 进行解析创建 url

url(url context, string spec, urlstreamhandler handler)

通过在指定的上下文中用指定的处理程序对给定的 spec 进行解析来创建 url

返回值

int

getdefaultport()

获取与此 url 关联协议的默认端口号

string

getfile()

获取此 url 的文件名

gethost()

获取此 url 的主机名(如果适用)

getpath()

获取此 url 的路径部分

getport()

获取此 url 的端口号

getprotocol()

获取此 url 的协议名称

getquery()

获取此 url 的查询部分

getref()

获取此 url 的锚点(也称为“引用”)

getuserinfo()

获取此 url 的 userinfo 部分

urlconnection

openconnection()

返回一个 urlconnection 对象,它表示到 url 所引用的远程对象的连接

openconnection(proxy proxy)

与 openconnection()类似,所不同是连接通过指定的代理建立;不支持代理方式的协议处理程序将忽略该代理参数并建立正常的连接

inputstream

openstream()

打开到此 url 的连接并返回一个用于从该连接读入的 inputstream

static void

seturlstreamhandlerfactory(urlstreamhandlerfactory fac)

设置应用程序的 urlstreamhandlerfactory

toexternalform()

构造此 url 的字符串表示形式

tostring()

uri

touri()

返回与此 url 等效的 uri

抽象类urlconnection是所有类的超类,它代表应用程序和url之间的通信链接。此类的实例可用于读取和写入此url引用的资源。通常,创建一个到 url的连接需要几个步骤:

通过在 url 上调用 openconnection 方法创建连接对象。

处理设置参数和一般请求属性。

使用 connect 方法建立到远程对象的实际连接。

远程对象变为可用。远程对象的头字段和内容变为可访问

使用以下方法修改设置参数:

setallowuserinteraction

setdoinput

setdooutput

setifmodifiedsince

setusecaches

使用以下方法修改一般请求属性:

setrequestproperty

使用 setdefaultallowuserinteraction 和 setdefaultusecaches 可设置 allowuserinteraction 和 usecaches 参数的默认值。

上面每个 set 方法都有一个用于获取参数值或一般请求属性值的对应 get 方法。适用的具体参数和一般请求属性取决于协议。

在建立到远程对象的连接后,以下方法用于访问头字段和内容:

getcontent

getheaderfield

getinputstream

getoutputstream

某些头字段需要经常访问。以下方法:

getcontentencoding

getcontentlength

getcontenttype

getdate

getexpiration

getlastmodifed

提供对这些字段的便捷访问。getcontent 方法使用 getcontenttype 方法以确定远程对象类型;子类重写 getcontenttype 方法很容易。

一般情况下,所有的预连接参数和一般请求属性都可忽略:预连接参数和一般请求属性默认为敏感值。对于此接口的大多数客户端而言,只有两个需要的方法:getinputstream 和 getcontent,它们通过便捷方法镜像到 url 类中。

void

addrequestproperty(string key, string value)

添加由键值对指定的一般请求属性

abstract void

connect()

打开到此 url 引用的资源的通信链接(如果尚未建立这样的连接)

boolean

getallowuserinteraction()

返回此对象的allowuserinteraction字段的值

getconnecttimeout()

返回连接超时设置

object

getcontent()

获取此 url 连接的内容

getcontent(class[] classes)

getcontentencoding()

返回 content-encoding 头字段的值

getcontentlength()

返回 content-length 头字段的值

getcontenttype()

返回 content-type 头字段的值

long

getdate()

返回 date 头字段的值

getdefaultusecaches()

返回 urlconnection 的 usecaches 标志的默认值

getdoinput()

返回此 urlconnection 的 doinput 标志的值

getdooutput()

返回此 urlconnection 的 dooutput 标志的值

getexpiration()

返回 expires 头字段的值

getheaderfield(int n)

返回第 n 个头字段的值

getheaderfield(string name)

返回指定的头字段的值

getheaderfielddate(string name, long default)

返回解析为日期的指定字段的值

getheaderfieldint(string name, int default)

返回解析为数字的指定字段的值

getheaderfieldkey(int n)

返回第 n 个头字段的键

map<string,list<string>>

getheaderfields()

返回头字段的不可修改的 map

getifmodifiedsince()

返回此对象的 ifmodifiedsince 字段的值

getinputstream()

返回从此打开的连接读取的输入流

getlastmodified()

返回 last-modified 头字段的值

outputstream

getoutputstream()

返回写入到此连接的输出流

getreadtimeout()

返回读入超时设置

getrequestproperties()

返回一个由此连接的一般请求属性构成的不可修改的 map

getrequestproperty(string key)

返回此连接指定的一般请求属性值

url

geturl()

返回此 urlconnection 的 url 字段的值

getusecaches()

返回此 urlconnection 的 usecaches 字段的值

static string

guesscontenttypefromname(string fname)

根据 url 的指定 “file” 部分尝试确定对象的内容类型

guesscontenttypefromstream(inputstream is)

根据输入流的开始字符尝试确定输入流的类型

setallowuserinteraction(boolean allowuserinteraction)

设置此 urlconnection 的 allowuserinteraction 字段的值

setconnecttimeout(int timeout)

设置一个指定的超时值(以毫秒为单位),该值将在打开到此 urlconnection 引用的资源的通信链接时使用

setcontenthandlerfactory(contenthandlerfactory fac)

设置应用程序的 contenthandlerfactory

setdefaultallowuserinteraction(boolean defaultallowuserinteraction)

将未来的所有 urlconnection 对象的 allowuserinteraction 字段的默认值设置为指定的值

setdefaultusecaches(boolean defaultusecaches)

将 usecaches 字段的默认值设置为指定的值

setdoinput(boolean doinput)

将此 urlconnection 的 doinput 字段的值设置为指定的值

setdooutput(boolean dooutput)

将此 urlconnection 的 dooutput 字段的值设置为指定的值

setfilenamemap(filenamemap map)

设置 filenamemap

setifmodifiedsince(long ifmodifiedsince)

将此 urlconnection 的 ifmodifiedsince 字段的值设置为指定的值

setreadtimeout(int timeout)

将读超时设置为指定的超时值,以毫秒为单位

setrequestproperty(string key, string value)

设置一般请求属性

setusecaches(boolean usecaches)

将此 urlconnection 的 usecaches 字段的值设置为指定的值

返回此 url 连接的 string 表示形式

httpurlconnection是urlconnection的子类,所以urlconnection中的方法,httpurlconnection中都有,httpurlconnection对urlconnection进行了扩展。

每个 httpurlconnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 http 服务器的基础网络。请求后在 httpurlconnection 的 inputstream 或 outputstream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。

disconnect()

指示近期服务器不太可能有其他请求。

geterrorstream()

如果连接失败但服务器仍然发送了有用数据,则返回错误流。

static boolean

getfollowredirects()

返回指示是否应该自动执行 http 重定向 (3xx) 的 boolean 值。

返回 nth 头字段的值。

返回解析为日期的指定字段的值。

返回 nth 头字段的键。

getinstancefollowredirects()

返回此 httpurlconnection 的 instancefollowredirects 字段的值。

getrequestmethod()

获取请求方法。

getresponsecode()

从 http 响应消息获取状态码。

getresponsemessage()

获取与来自服务器的响应代码一起返回的 http 响应消息(如果有)。

setchunkedstreamingmode(int chunklen)

此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 http 请求正文的流。

setfixedlengthstreamingmode(int contentlength)

此方法用于在预先已知内容长度时启用没有进行内部缓冲的 http 请求正文的流。

setfollowredirects(boolean set)

设置此类是否应该自动执行 http 重定向(响应代码为 3xx 的请求)。

setinstancefollowredirects(boolean followredirects)

设置此 httpurlconnection 实例是否应该自动执行 http 重定向(响应代码为 3xx 的请求)。

setrequestmethod(string method)

设置 url 请求的方法, get post head options put delete trace 以上方法之一是合法的,具体取决于协议的限制。

abstract boolean

usingproxy()

指示连接是否通过代理

修改上面的示例如下: