Java URL openConnection() 方法

2025年3月24日 | 阅读时间:2 分钟

URL 类的 openConnection() 方法打开到指定 URL 的连接,并返回一个 URLConnection 实例,该实例代表到 URL 所引用远程对象的连接。

应注意,URLConnection 实例在创建时并不会建立实际的网络连接。这仅在调用 URLConnection.connect() 时才会发生。

签名

openConnection 方法根据下表重载。

编号。方法描述
1.URLConnection openConnection()返回一个 URLConnection 实例,该实例代表到 URL 所引用远程对象的连接。
2.openConnection(Proxy proxy) throws IOException与 openConnection() 相同,不同之处在于连接将通过指定的代理建立;不支持代理的协议处理器将忽略代理参数并建立正常连接。

返回值

指向 URL 的 URLConnection。

Throw

IOException - 如果发生 I/O 异常。

示例 1

输出

The mime type is : text/html; charset=iso-8859-1
 The time out time of connection is : 0

示例 2

输出

-----------------------------------
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>JDBC Tutorial | What is Java Database Connectivity(JDBC) - tpointtech</title><link rel="SHORTCUT ICON" href="/images/logo/icon.png" />
<link rel="stylesheet" type="text/css" href="/link.css"/>
<meta name="keywords" content="java, jdbc, tutorial, api, drivers, odbc"/><meta name="description" content="Java JDBC Tutorial or What is Java Database Connectivity(JDBC) with examples on Driver, DriverManager, Connection, Statement, ResultSet, PreparedStatement, CallableStatement, ResultSetMetaData, DatabaseMetaData, RowSet, Store Image, Fetch Image, Store file, Fetch file etc."/><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="canonical" href="https://tpointtech.cn/java-jdbc" />
下一个主题Java URL 类