我有一个数据库,需要做一个真正的职位。我做了一个很好的get和post是失败的(我不知道在哪里),在编译上没有问题。
POST方法失败:
URL url = new URL(urlPost);
HttpURLConnection conexionPost = (HttpURLConnection) url.openConnection();
conexionPost.setDoOutput(true);
conexionPost.setRequestMethod("POST");
conexionPost.setRequestProperty("Accept-Encoding", "gzip,deflate");
conexionPost.setRequestProperty("Content-Length", "216");
conexionPost.connect();
conexionPost.disconnect();
以及可以使用的get方法:
String sGet = "xxxxx:2480/query/mydb/sql/...";
URL urlGet = new URL(sGet);
HttpURLConnection conexionGet = (HttpURLConnection) urlGet.openConnection();
conexionGet.setDoInput(true);
conexionGet.setRequestMethod("GET");
BufferedReader in1 = new BufferedReader(new InputStreamReader(conexionGet.getInputStream()));
String texto = "";
String request = "";
while ((texto = in1.readLine()) != null) {
request += texto;
}
in1.close();
System.out.println(request);
我的create vertex的sql代码如下:
String urlPost = urlServer + "/command/mydb/sql/CREATE%20VERTEX%20V%20"
+ "SET%20certificateFingerprint%20=%20%27" + datos[9]+ "%27";
db的答案是:{“result”:[]},而我的db是空的(显然)。
提前谢谢。
最佳答案
您的post请求中缺少身份验证。
我试过这个代码,效果很好:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
public class Stack38089384 {
public static void main(String[] args) throws IOException {
String urlPost = "http://localhost:2480/command/Stack38089384/sql/create%20class%20Test%20extends%20v";
URL url = new URL(urlPost);
HttpURLConnection conexionPost = (HttpURLConnection)url.openConnection();
String userCredentials = "root:root";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
conexionPost.setRequestProperty ("Authorization", basicAuth);
conexionPost.setRequestMethod("POST");
conexionPost.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conexionPost.setRequestProperty("Content-Language", "en-US");
conexionPost.setUseCaches(false);
conexionPost.setDoInput(true);
conexionPost.setDoOutput(true);
System.out.println(conexionPost.getResponseCode());
}
}
希望有帮助。