Page 1 of 1
Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Mon Dec 02, 2013 8:13 pm
by socialguy
Given the following JAX-RS client:
Code: Select all
public class URLConClient {
public static void main(String[] args) throws IOException {
URL restURL = new URL("http://localhost:8080/SimpleRS/jax/rs/add/5/6");
URLConnection connection = (URLConnection) restURL.openConnection();
// 1
readFrom(connection);
}
}
What are connection setup methods that can be in line //1?
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setReadTimeout(10000);
connection.setDoOutput(true);
connection.setDoInput(false);
connection.setDoOutput(true) is not required as we're not writing out anything from the client. The explanation says the same thing as well but still it's marked as one of the correct options. Why?
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Tue Dec 03, 2013 2:00 am
by fjwalraven
connection.setDoOutput(true) is not required as we're not writing out anything from the client. The explanation says the same thing as well but still it's marked as one of the correct options. Why?
You are right,
setDoOutput(true) is not required. It is a bit of trick question though.
Basically it doesn't harm calling this method. Note that the question states:
"What are connection setup methods that can be in line //1?". It is not stating "should" or "must".
The options
connection.setDoInput(false) and the
connection.setRequestMethod("GET") do harm.
Regards,
Frits
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Tue Dec 03, 2013 11:33 am
by socialguy
Thanks. You may want to update the answer explanation in the simulator to what you said here; it's a little confusing there because it just states that the method call is not needed. Leaves the reader wondering why the answer is correct then.
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Wed Dec 04, 2013 4:04 pm
by fjwalraven
Good point!
I have changed the explanation.
Thanks,
Frits
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Tue Apr 08, 2014 11:58 am
by himaiMinh
One more point, it is optional to have this
According to MZ's notes, the flag for setDoInput is true by default. If the client wants to get input, this line of code can be omitted. According to MZ, "The client code will be able to read HTTP response by default."
One more interesting finding,usually most examples from books do this:
Code: Select all
//The fully qualified name is java.net.HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) restURL.openConnection();
But in this question, the connection is URLConnection:
Code: Select all
//URLConnection is an abstract class and I wonder what connection's concrete type is....
URLConnection connection = (URLConnection)restURL.openConnection();
Then, I do this
Code: Select all
System.out.println(connection.getClass().getName());
The output is : sun.net.
www.protocol.http.HttpURLConnection
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Tue Apr 08, 2014 12:08 pm
by himaiMinh
Another point is, I think it does not harm to put
Code: Select all
connection.setAllowUserInteraction (true);
It compiles, but it is just not necessary.
Re: Question enthuware.ocejws.v6.2.204: wrong answer?
Posted: Tue Apr 08, 2014 1:51 pm
by fjwalraven
himaiMinh wrote:One more interesting finding,usually most examples from books do this:
Code: Select all
//The fully qualified name is java.net.HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) restURL.openConnection();
But in this question, the connection is URLConnection:
Code: Select all
//URLConnection is an abstract class and I wonder what connection's concrete type is....
URLConnection connection = (URLConnection)restURL.openConnection();
Then, I do this
Code: Select all
System.out.println(connection.getClass().getName());
The output is : sun.net.
www.protocol.http.HttpURLConnection
The HttpURLConnection extends URLConnection and both (!) classes are abstract.
Regards,
Frits