I am new in java and I am still dealing with the basic topics. I cannot really understand how the following lines work when obtaining a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
How can you instantiate the HttpURLConnection with the URL object if URL class does not inherit from HttpURLConnection?
According to the java API reference this is the hierarchy of both classes:
Java.lang.Object ⇒ java.net.URLConnection ⇒ java.net.HttpURLConnection
java.lang.Object ⇒ java.net.URL
As far as I know casting is used in the following way:
In this case HttpURLConnection is descended from URLConnection and Object.
Therefore, a HttpURLConnection is a URLConnection and is also an Object.
The reverse is not necessarily true: a URLConnection may be a HttpURLConnection, but it isn't necessarily. So you have to use casting.
But URL class and HttpURLConnection ARE NOT related. Only trough OBJECT class.
That is what I cannot understand. Can someone help me?
Thank you in advance.
You're not casting URL, you're casting the return value of openConnection() (URLConnection), which is deep down a HttpURLConnection when the address starts with http://.
In the URL class is stated as follow for the 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.
Related
I am trying to understand when to use URLConnection Class & when to use HttpURLConnection class. On doing some research, I came to know that URLConnection is used for nonHTTP connections & HttpURLConnection is used for specific HTTP connections. Can someone help me to know that what these nonHTTP connections refer to ?
PS - Please note that my qn is not regarding inheritance relationship of URLConnection Class & HttpURLConnection class.
It is regarding what do we mean by nonHTTP connections (that can be handled by URLConnection Class). Is nonHTTP connection means a datagram connection or ftp connection or something else.Refer below website:
https://www.experts-exchange.com/questions/21420009/URLConnection-and-HttpURLConnection.html
A URLConnection is an abstract class, representing any connection to an URL.
A HttpURLConnecton IS a URLConnection (subclass) and offers more methods.
That being said, you could open a URL with the HTTP protocol and assign the return value to a URLConnection, but you'd be missing the more specific methods.
You can open a URL to an ftp resource, or anything else and assign the result to a URLConnection, but it might not support the underlying protocol. For example, FTP support is very limited.
See the documentation for 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.
So, yes, use HttpURLConnection for HTTP connection and any specialized URLConnection you have, or use the raw URLConnection otherwise. That being said, it depends with which protocol you want to work - you might need a more complete client for any non-trivial protocol.
I use the class HttpURLConnection , but Iam confused with some methods
assume the HttpURLConnection object name is "c"
removing c.connect() will result in a success connection and it will retrieve the connection result without any problem
the output of c.getInputStream() and (InputStream) c.grtContent() are identical , so what is the difference betqween them
using HttpGet will reach the same approach as HttpURLConnection , so what is the difference between the two methods
what are the extra benefits for HttpURLConnection on URLConnection
c = (HttpURLConnection) (URL).openConnection();
c.connect(); //adding or removing makes the same result , so what is the usage of this method
InputStream stream= c.getInputStream();
InputStream stream2 = (InputStream) c.getContent();
//stream and stream2 are identical , so what is the differece between getInputStream() and getContent()
//============================
HttpGet c= new HttpGet(url);
HttpResponse response = c.execute(httpGet)
InputStream stream3 = response.getEntity().getContent();
//also stream3 is the same as stream & stream2 ; so how dose it different between HttpGet & HttpURLConnection
From Android documentation:
[HttpURLConnection is] A URLConnection with support for HTTP-specific features.
For instance, from an HttpURLConnection you can retrieve the HTTP method or the HTTP status code, which are HTTP-specific.
The URLConnection class, instead, is:
The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL
The normal usage is:
Create a URL object
Get a URLConnection by calling url.openConnection(). The returned object can be casted to an HttpURLConnection
get an InputStream by calling the connection.getInputStream() method
Close the connection (disconnect() method) (see #EJP comments)
Concerning the connect() method, from the Oracle documentation:
You are not always required to explicitly call the connect method to initiate the connection. Operations that depend on being connected, like getInputStream, getOutputStream, etc, will implicitly perform the connection, if necessary.
The difference between the HttpGet and HttpURLConnection is that they belong to 2 different libraries, but functionally they are more or less the same (now HttpGet has been deprecated and removed, so you won't find it in the standard Android APIs)
I have a question regarding using an HttpUrlConnection with a https web service.
Basically, we had previously written a bunch of web services to be used in an Android application that were all http calls. We want to change those over to use https instead. Basically, what I'm concerned with, is will my existing code work properly? I had previously created a connection like the following:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.connect();
This appears to be working fine with the new https web services, but I'm wondering why I don't need to change it to something like this:
HttpsURLConnection myConnection = (HttpsURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.setSSLSocketFactory(createSSLSocketFactory());
myConnection.connect();
I'm still planning on moving to a HttpsURLConnection instead, but I'm curious how older versions of our app will be affected by our intended changes.
Thanks for the help!
In docs for HttpUrlConnection you will find:
Calling openConnection() on a URL with the "https" scheme will return
an HttpsURLConnection
But since HttpsUrlConnection extends from HttpUrlConnection, this code:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
will NOT result in class cast exception if url is for https connection. You still can cast to HttpsUrlConnection - just for safety you can check with instanceof if url scheme should change.
I need to create a simple HTTP client program with Java.
I've not found any example of implementation in Java that allows calling the OPTIONS method to get the Allow header with the allowed methods on the server.
I tried using:
HttpURLConnection http = (HttpURLConnection) url.openConnection();
System.out.println(http.getHeaderFields());
But the field Allow: GET, POST ... is not included.
The connection object fires a GET request by default. You need to set the request method to OPTIONS.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getRequestMethod()); // GET
conn.setRequestMethod("OPTIONS");
System.out.println(conn.getHeaderField("Allow")); // depends
I am currently trying to understand the logic behind each of the code below. Please let me know if I am correct and answer my confusions.
urlConnection = (HttpURLConnection) url.openConnection();
//connection object is created. However, do not understand
//"manipulate parameters that affect the connection to the remote resource."
urlConnection.setRequestMethod("GET");
//set the method for the URL request. Not sure what that means!
//there are other strings that could be used, but not sure what each means,
//and couldn't the Java documentations didn't seem to have explanations either
urlConnection.connect();
//the actual connection the the remote object is made
This question is not related to Java at all. What you need is to understand HTTP protocol and do some comprehensive reading. I recommend starting with
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
and then read:
https://www.rfc-editor.org/rfc/rfc7230