java URLConnection获取连接数据属性相关操作
———–URLConnection获取连接数据属性:
package com.mldn;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.io.IOException;
public class URLConnect
{
public static void main(String[] args)
{
URL url = null; // 声明URL
URLConnection urlConnect = null; // 声明URL连接
try
{
url = new URL(“https://www.k187.com”); // 默认是首页
}
catch (MalformedURLException e)
{
e.printStackTrace(); // 不良的URL异常
}
try
{
urlConnect = url.openConnection(); // 获取该URL连接
}
catch (IOException e)
{
e.printStackTrace(); // 获取该网址输入流
}
System.out.println(“连接内容大小:” + urlConnect.getContentLength());
System.out.println(“连接内容类型:” + urlConnect.getContentType());
System.out.println(“连接内容编码:” + urlConnect.getContentEncoding());
System.out.println(urlConnect.getHeaderField(1));
}
}
/*
ubuntu@xu-desktop:~$ java com.mldn.URLConnect
连接内容大小:-1
连接内容类型:text/html; charset=gbk
连接内容编码:null
close
// URLConnection 可以获取该连接的一些属性!
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java URLConnection获取连接数据属性相关操作》
本文地址:http://www.xiupu.net/archives-173.html
关注公众号:
微信赞赏
支付宝赞赏