接口 | 描述 |
---|---|
ContentHandlerFactory |
该界面为内容处理程序定义了一个工厂。
|
CookiePolicy |
CookiePolicy实现决定哪些Cookie应该被接受,哪些应该被拒绝。
|
CookieStore |
CookieStore对象表示Cookie的存储。
|
DatagramSocketImplFactory |
该接口为数据报套接字实现定义了一个工厂。
|
FileNameMap |
一个简单的界面,提供了一种在文件名和MIME类型字符串之间映射的机制。
|
ProtocolFamily |
代表一系列通信协议。
|
SocketImplFactory |
该接口定义了一个用于套接字实现的工厂。
|
SocketOption<T> |
与套接字关联的套接字选项。
|
SocketOptions |
获取/设置套接字选项的方法的接口。
|
URLStreamHandlerFactory |
该接口为
URL 流协议处理程序定义了一个工厂。
|
类 | 描述 |
---|---|
Authenticator |
Authenticator类代表一个知道如何获取网络连接认证的对象。
|
CacheRequest |
表示在ResponseCache中存储资源的通道。
|
CacheResponse |
代表从ResponseCache中检索资源的通道。
|
ContentHandler |
抽象类
ContentHandler 是读取所有类的超
Object 从
URLConnection 。
|
CookieHandler |
CookieHandler对象提供一个回调机制,将HTTP状态管理策略实现连接到HTTP协议处理程序中。
|
CookieManager |
CookieManager提供了一个具体的 CookieHandler 实现 ,它将cookie的存储与接受和拒绝cookie的策略分开。
|
DatagramPacket |
该类表示数据报包。
|
DatagramSocket |
此类表示用于发送和接收数据报数据包的套接字。
|
DatagramSocketImpl |
抽象数据报和组播套接字实现基类。
|
HttpCookie |
HttpCookie对象表示HTTP cookie,它承载服务器和用户代理之间的状态信息。
|
HttpURLConnection |
支持HTTP特定功能的URLConnection。
|
IDN |
提供在正常Unicode表示和ASCII兼容编码(ACE)表示之间转换国际化域名(IDN)的方法。
|
Inet4Address |
该类表示Internet协议版本4(IPv4)地址。
|
Inet6Address |
该类表示Internet协议版本6(IPv6)地址。
|
InetAddress |
此类表示Internet协议(IP)地址。
|
InetSocketAddress |
该类实现IP套接字地址(IP地址+端口号)它也可以是一对(主机名+端口号),在这种情况下将尝试解析主机名。
|
InterfaceAddress |
此类表示网络接口地址。
|
JarURLConnection |
与Java ARchive(JAR)文件或JAR文件中的条目的URL连接。
|
MulticastSocket |
组播数据报套接字类对发送和接收IP组播数据包很有用。
|
NetPermission |
此类用于各种网络权限。
|
NetworkInterface |
此类表示由名称组成的网络接口和分配给此接口的IP地址列表。
|
PasswordAuthentication |
PasswordAuthentication类是Authenticator使用的数据持有者。
|
Proxy |
此类表示代理设置,通常是类型(http,socks)和套接字地址。
|
ProxySelector |
选择在连接到URL引用的网络资源时使用的代理服务器(如果有)。
|
ResponseCache |
表示URLConnection缓存的实现。
|
SecureCacheResponse |
表示最初通过安全手段(例如TLS)检索的缓存响应。
|
ServerSocket |
这个类实现了服务器套接字。
|
Socket |
该类实现客户端套接字(也称为“套接字”)。
|
SocketAddress |
这个类代表一个没有协议附件的Socket地址。
|
SocketImpl |
抽象类
SocketImpl 是实际实现套接字的所有类的公共超类。
|
SocketPermission |
此类表示通过套接字访问网络。
|
StandardSocketOptions |
定义
标准套接字选项。
|
URI |
表示统一资源标识符(URI)参考。
|
URL |
URL 类
URL 统一资源定位器,指向万维网上的“资源”的指针。
|
URLClassLoader |
此类加载器用于从引用JAR文件和目录的URL的搜索路径加载类和资源。
|
URLConnection |
抽象类
URLConnection 是表示应用程序和URL之间的通信链接的所有类的超类。
|
URLDecoder |
HTML表单解码的实用类。
|
URLEncoder |
HTML表单编码的实用类。
|
URLPermission |
表示访问由给定网址定义的资源或资源集的权限,以及给定的用户可设置请求方法和请求标头集合的权限。
|
URLStreamHandler |
抽象类
URLStreamHandler 是所有流协议处理程序的通用超类。
|
Enum | 描述 |
---|---|
Authenticator.RequestorType |
请求认证的实体的类型。
|
Proxy.Type |
代表代理类型。
|
StandardProtocolFamily |
定义通信协议的标准系列。
|
异常 | 描述 |
---|---|
BindException |
指示尝试将套接字绑定到本地地址和端口时发生错误。
|
ConnectException |
指示尝试将套接字连接到远程地址和端口时发生错误。
|
HttpRetryException |
抛出表示HTTP请求需要重试,但由于启用了流式传输模式,因此无法自动重试。
|
MalformedURLException |
抛出以表示发生格式不正确的网址。
|
NoRouteToHostException |
指示尝试将套接字连接到远程地址和端口时发生错误。
|
PortUnreachableException |
指示在连接的数据报上收到了ICMP端口不可达消息。
|
ProtocolException |
抛出以表示底层协议有错误,如TCP错误。
|
SocketException |
抛出以表示创建或访问Socket时出错。
|
SocketTimeoutException |
指示在套接字上发生超时时读取或接受。
|
UnknownHostException |
抛出以表示无法确定主机的IP地址。
|
UnknownServiceException |
抛出以表示发生未知服务异常。
|
URISyntaxException |
检查的异常抛出以指示不能将字符串解析为URI引用。
|
The java.net package can be roughly divided in two sections:
A Low Level API, which deals with the following abstractions:
Addresses, which are networking identifiers, like IP addresses.
Sockets, which are basic bidirectional data communication mechanisms.
Interfaces, which describe network interfaces.
A High Level API, which deals with the following abstractions:
URIs, which represent Universal Resource Identifiers.
URLs, which represent Universal Resource Locators.
Connections, which represents connections to the resource pointed to by URLs.
Addresses are used throughout the java.net APIs as either host identifiers, or socket endpoint identifiers.
The InetAddress
class is the abstraction representing an IP (Internet Protocol) address. It has two subclasses:
Inet4Address
for IPv4 addresses.Inet6Address
for IPv6 addresses.But, in most cases, there is no need to deal directly with the subclasses, as the InetAddress abstraction should cover most of the needed functionality.
Not all systems have support for the IPv6 protocol, and while the Java networking stack will attempt to detect it and use it transparently when available, it is also possible to disable its use with a system property. In the case where IPv6 is not available, or explicitly disabled, Inet6Address are not valid arguments for most networking operations any more. While methods like InetAddress.getByName(java.lang.String)
are guaranteed not to return an Inet6Address when looking up host names, it is possible, by passing literals, to create such an object. In which case, most methods, when called with an Inet6Address will throw an Exception.
Sockets are means to establish a communication link between machines over the network. The java.net package provides 4 kinds of Sockets:
Socket
is a TCP client API, and will typically be used to connect to a remote host.ServerSocket
is a TCP server API, and will typically accept connections from client sockets.DatagramSocket
is a UDP endpoint API and is used to send and receive datagram packets.MulticastSocket
is a subclass of DatagramSocket
used when dealing with multicast groups.Sending and receiving with TCP sockets is done through InputStreams and OutputStreams which can be obtained via the Socket.getInputStream()
and Socket.getOutputStream()
methods.
The NetworkInterface
class provides APIs to browse and query all the networking interfaces (e.g. ethernet connection or PPP endpoint) of the local machine. It is through that class that you can check if any of the local interfaces is configured to support IPv6.
Note, all conforming implementations must support at least one NetworkInterface
object, which must either be connected to a network, or be a "loopback" interface that can only communicate with entities on the same machine.
A number of classes in the java.net package do provide for a much higher level of abstraction and allow for easy access to resources on the network. The classes are:
URI
is the class representing a Universal Resource Identifier, as specified in RFC 2396. As the name indicates, this is just an Identifier and doesn't provide directly the means to access the resource.URL
is the class representing a Universal Resource Locator, which is both an older concept for URIs and a means to access the resources.URLConnection
is created from a URL and is the communication link used to access the resource pointed by the URL. This abstract class will delegate most of the work to the underlying protocol handlers like http or https.HttpURLConnection
is a subclass of URLConnection and provides some additional functionalities specific to the HTTP protocol.The recommended usage is to use URI
to identify resources, then convert it into a URL
when it is time to access the resource. From that URL, you can either get the URLConnection
for fine control, or get directly the InputStream.
Here is an example:
URI uri = new URI("http://java.sun.com/"); URL url = uri.toURL(); InputStream in = url.openStream();
myproto://myhost.mydomain/resource/
), a similar URL will try to instantiate the handler for the specified protocol; if it doesn't exist an exception will be thrown.
By default the protocol handlers are loaded dynamically from the default location. It is, however, possible to add to the search path by setting the java.protocol.handler.pkgs
system property. For instance if it is set to myapp.protocols
, then the URL code will try, in the case of http, first to load myapp.protocols.http.Handler
, then, if this fails, http.Handler
from the default location.
Note that the Handler class has to be a subclass of the abstract class URLStreamHandler
.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.