Page 1 of 1

About Question enthuware.ocejws.v6.2.209 :

Posted: Thu Jul 31, 2014 2:27 pm
by Kanwaljit
What is the correct sequence of methods that must be added (in line //1 and line //2) in the following JAX-RS client to return a valid result. The request should be a POST request with a String as body.
public static void main(String[] args) throws IOException {   
URL restURL = new URL("http://localhost:8080/SimpleRS/math/table/post");    
HttpURLConnection connection = (HttpURLConnection) restURL.openConnection();   
 // 1    
connection.connect();    
String entity = "5";   
 // 2
}


Why is this wrong:
Line // 1 connection.setRequestMethod("POST");
connection.setDoOutput(true);
Line // 2
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeChars(entity);
wr.flush(); wr.close();

why this is right:
Line // 1
connection.setRequestMethod("POST");
connection.setDoOutput(true);
Line // 2
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(entity);
wr.flush(); wr.close();

Re: About Question enthuware.ocejws.v6.2.209 :

Posted: Fri Aug 01, 2014 12:10 am
by fjwalraven
Hi,

The underlying stream of a URLConnection is a byte-based stream, that is why you have to use the writeBytes() method. Just try and see if you use the writeChars() method.

I will update the explanation to the question to make it more clear.

Thanks for your feedback!

Regards,
Frits