5

I am working on a REST Client experimenting with the tinkerpop database using using HttpURLConnection.

I am trying to send over a 'GET - CONNECT'. Now I understand (from some net research) that if I use doOutput(true) the 'client' will a 'POST' even if I setRequestMethod 'GET' as POST is the default (well ok?) however when I comment out the the doOutput(true) I get this error:

java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:995) at RestContent.handleGetConnect(RestContent.java:88) at RestClient.main(RestClient.java:42)` 

Here is the communication code snip I have tried various option with setUseDoOutPut().

//connection.setDoInput(true); connection.setUseCaches (false); connection.setDoOutput(true); connection.setAllowUserInteraction(false); // set GET method try { connection.setRequestMethod("GET"); } catch (ProtocolException e1) { e1.printStackTrace(); connection.disconnect(); } 

Exception at connection.setRequestMethod("GET") in the other case. Any hints?

3
  • try conn.setDoOutput(false); Also see: stackoverflow.com/questions/6341602/… Commented Sep 25, 2013 at 15:27
  • What do you want to send? Is GET - CONNECT the body you want to send? Commented Jan 2, 2014 at 11:14
  • Why are you writing a body to a GET request? What is wrong with using POST? Commented Jun 18, 2019 at 7:08

1 Answer 1

2

Following code works fine: URL is a Rest URL with supported GET operation.

 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); //connection.setDoOutput(true); InputStream content = (InputStream) connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.