今天看了一下公司同事的代码,如下
URLConnection connection = openConnection(localURL);
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
好奇,为何需要将 URLConnection 转 HttpURLConnection ?
点进两个源码里面发现
一、HttpURLConnection 继承 URLConnection
public abstract class URLConnection //URLConnection
abstract public class HttpURLConnection extends URLConnection //HttpURLConnection
将转换的代码注掉,发现编译报错,证明
二、URLConnection 较 HttpURLConnection 多一些方法,如图下的getResponseCode()
再去仔细看 两个类分别得详解,个人理解:
三、URLConnection 可以走邮件、文件传输协议,而HttpURLConnection 就单指浏览器的HTTP协议
==================分割线====================
这是我搜到的博友写的,区别