URLconnection for nonHTTP connections - java

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.

Related

Casting an URL object to HttpURLConnection

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.

HttpURLConnection, openConnection and setRequestMethod("GET"

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

How to get HTTP request string from HttpURLConnection instance

I have HttpURLConnection instance created from URL and also I set query parameters and called some setters on this HttpURLConnection instance. I use this instance to get response from web service.
Is there some way to get the HTTP request string that will be sent over the network when using the given HttpURLConnection instance ? (just for debugging purposes). Can we do this programatically using HttpURLConnection or if it's not possible how can I monitor the outgoing HTTP traffic ?
The reason I need this that in some cases it can be easier to detect what is wrong with your configuration of HttpURLConnection by looking directly at the request that is defined by this instance than trying to figure out what is wrong with particular configuration of HttpURLConnection by checking what setters was called.
Thank you for any suggestion.
You can monitor your http Traffic by using Fiddler
Here is the download link

Use Server cache for 15 minutes

I want to use server cache for 15 minutess so what i have to use in setRequestProperty() ?
Please Help me..
Here is my code which i used..
private HttpURLConnection httpCon = null;
httpCon = (HttpURLConnection) httpUrl.openConnection();
httpCon.setRequestMethod("GET");
httpCon.setRequestProperty("Connection", "Keep-Alive");
httpCon.setRequestProperty("Pragma","public");
httpCon.setRequestProperty("Cache-Control","maxage=900");
httpCon.setUseCaches(true);
You are telling the server you are willing for it to cache responses, but there's no guarantee that the server will do that or is enabled to do that (unless you control the server also and implement that).
You can also try setting up an intermediate HTTP cache the client and server, such as a proxy cache such as Varnish, Pound, or Squid.
Lastly, you can do browser caching on your own, which is supported the the Android java.net package but doesn't have a default implementation. To do this:
-Check out HttpURLConnection which details (in the "Response Caching" section) that you must implement ResponseCache and call setDefault.
-Also check out ResponseCache Example which has examples of this, and something quirky to watch out for at the end (which may or may not still be true).
Good luck!
Instead of using the HttpConnection, use DefaultHttpClient and CachingHttpClient (part of Apache Http Client, bundled by default with Android).
Have a look at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html to get more details on how to use caching.

Alternative to java.net.URL for custom timeout setting

Need timeout setting for remote data request made using java.net.URL class. After some googling found out that there are two system properties which can be used to set timeout for URL class as follows.
sun.net.client.defaultConnectTimeout
sun.net.client.defaultReadTimeout
I don't have control over all the systems and don't want everybody to keep setting the system properties. Is there any other alternative for making remote request which will allow me to set timeouts.
Without any library, If available in java itself is preferable.
If you're opening a URLConnection from URL you can set the timeouts this way:
URL url = new URL(urlPath);
URLConnection con = url.openConnection();
con.setConnectTimeout(connectTimeout);
con.setReadTimeout(readTimeout);
InputStream in = con.getInputStream();
How are you using the URL or what are you passing it to?
A common replacement is the Apache Commons HttpClient, it gives much more control over the entire process of fetching HTTP URLs.

Categories

Resources