我正在使用HttpURLConnection
在REST客户端上尝试使用tinkerpop数据库。
我试图发送一个'GET - CONNECT'
。现在我理解(从一些网络研究),如果我使用doOutput(true)
''客户端'将'POST'即使我setRequestMethod
'GET'
作为POST是默认的(好吧?)但是当我注释掉doOutput(true)
我得到这个错误:
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)`
这是我用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();
}
在另一种情况下,connection.setRequestMethod("GET")
例外。任何提示?
以下代码正常工作:URL是具有受支持的GET操作的Rest URL。
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);
}