URLConnection 常用方法概述
URLConnection 常用方法:
- getHeaderField(String name): 获取指定头部字段的值。
- getInputStream(): 获取用于读取连接的输入流。
- getContentEncoding(): 获取
Content-Encoding
头部字段的值。 - getContentLength(): 获取
Content-Length
头部字段的值。 - getContentType(): 获取
Content-Type
头部字段的值。 - getDate(): 获取
Date
头部字段的值。
示例代码:
import java.net.URLConnection;
public class URLConnectionApp {
public static void main(String[] args) {
URLConnection connection = new URL("https://example.com").openConnection();
// 获取头部信息
String contentType = connection.getContentType();
int contentLength = connection.getContentLength();
// 获取输入流
InputStream inputStream = connection.getInputStream();
}
}
1MB
文件大小:
评论区