[ACCEPTED]-In java, how to create HttpsURLConnection or HttpURLConnection based on the url?-urlconnection
Accepted answer
HttpsURLConnection extends HttpUrlConnection, so you do not need the HttpsUrlConnection, you 1 can just do
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
since HttpsURLConnection extends HttpURLConnection
you can declare conn
as HttpsURLConnection
. In this way 4 you can access the common interface (setRequestMethod()
).
In 3 order to access the extension methods (like 2 getCipherSuite()
, defined only in the child class HttpsURLConnection
) you 1 must use a cast after an instanceof:
if (conn instanceof HttpsURLConnection) {
HttpsURLConnection secured = (HttpsURLConnection) conn;
String cipher = secured.getCipherSuite();
}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.