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