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();
About Question enthuware.ocejws.v6.2.209 :
Moderators: Site Manager, fjwalraven
-
- Posts: 1
- Joined: Thu Jul 31, 2014 12:19 pm
- Contact:
-
- Posts: 429
- Joined: Tue Jul 24, 2012 2:43 am
- Contact:
Re: About Question enthuware.ocejws.v6.2.209 :
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
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
Who is online
Users browsing this forum: No registered users and 4 guests