About Question enthuware.ocejws.v6.2.209 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Kanwaljit
Posts: 1
Joined: Thu Jul 31, 2014 12:19 pm
Contact:

About Question enthuware.ocejws.v6.2.209 :

Post 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();

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests