:あなたがurl.openConnection()
結果の型.Because URLConnection
に戻り値の型HttpsURLConnection
を変更する必要があり
public static HttpsURLConnection passmyurl(URL url) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
return connection;
}
がURLConnection
のサブタイプで、かつ正確なタイプはPARAMTERに依存url
のprotocol.Theのimplemention URL
クラスのopenConnection()
の文書:
If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.
ですから、Http
URLを渡したりすることができます01あなたのメソッドにのURLを入力してください。
URLConnection httpConnection = new URL("http://test").openConnection();
System.out.println(httpConnection.getClass());
URLConnection httpsConnection = new URL("https://test").openConnection();
System.out.println(httpsConnection.getClass());
URLConnection ftpConnection = new URL("ftp://test").openConnection();
System.out.println(ftpConnection.getClass());`
印刷は、次のとおりです:
class sun.net.www.protocol.http.HttpURLConnection
class sun.net.www.protocol.https.HttpsURLConnectionImpl
class sun.net.www.protocol.ftp.FtpURLConnection
は、次のコードを参照してください。