cURL DELETE on java

时间:2018-04-25 12:14:29

标签: java rest curl onos

我使用命令" curl -X DELETE --header'接受:application / json' ' http://10.10.1.29:8181/onos/v1/flows/application/olsrflow' -u karaf:karaf"

它可以工作。

但使用JAVA Code无法正常工作。

我的JAVA代码有问题吗?

     URL dc0ContrailUrl2 = new URL("http://10.10.1.29:8181/onos/v1/flows/application/olsrflow");
     HttpURLConnection dcConn2 = (HttpURLConnection) dc0ContrailUrl2.openConnection();
     dcConn2.setDoOutput(true);
     String login = "karaf:karaf";
     String content = URLEncoder.encode (login) ;
     String basicAuth = "Basic " + new String(new Base64().encode(login.getBytes()));
     dcConn2.setRequestProperty("Authorization",basicAuth);
     dcConn2.setRequestProperty("Content-Type", "application/json");
     dcConn2.setRequestMethod("DELETE");
     BufferedReader in2 = new BufferedReader(new InputStreamReader(dcConn2.getInputStream()));

     String inputLine2;

 while ((inputLine2 = in2.readLine()) != null){ //while response is not null, assign response to inputLine and print inputLine
         System.out.println(inputLine2); 
 }
         in2.close();

错误HTTP响应代码:415

2 个答案:

答案 0 :(得分:1)

您没有添加与curl相同的Accept类型。添加以下行并删除Content-Type:

 dcConn2.setRequestProperty("Accept", "application/json");

答案 1 :(得分:-1)

通常,这意味着服务器不支持已传递MediaType。你必须弄清楚它的期望,并相应地在你的请求中设置它。