Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用REST调用模拟cURL时出错_Java_Json_Rest_Curl - Fatal编程技术网

在java中使用REST调用模拟cURL时出错

在java中使用REST调用模拟cURL时出错,java,json,rest,curl,Java,Json,Rest,Curl,目前,我能够在curl中运行以下命令,该命令按预期返回json响应。请注意,输入JSON在请求中以名称JSON作为表单参数发送。API将只接受JSON请求,并且只支持POST方法调用 请求 curl --data 'json={ "startdate": "2012-10-10", "enddate":"2012-10-11" }' http://xxxxx.xxxx.xxx/xxxx/xxxx/xxxx/xxxx 回应 {"startdate":"2012-10-10","status":"

目前,我能够在curl中运行以下命令,该命令按预期返回json响应。请注意,输入JSON在请求中以名称JSON作为表单参数发送。API将只接受JSON请求,并且只支持POST方法调用

请求

curl --data 'json={ "startdate": "2012-10-10", "enddate":"2012-10-11" }' http://xxxxx.xxxx.xxx/xxxx/xxxx/xxxx/xxxx
回应

{"startdate":"2012-10-10","status":"true","Api Name":"xxxx","output":{"ROW":"49","EU":"54","NA":"33"},"enddate":"2012-10-11"}
我使用java中的REST调用模拟上述cURL命令

代码如下所示

String urlString = "http://xxxxx.xxxx.xxx/xxxx/xxxx/xxxx/xxxx";
URL myUrl = new URL(urlString);
URLConnection urlConn = myUrl.openConnection();

String myData = "json={ \"startdate\": \"2012-10-10\", \"enddate\":\"2012-10-11\" }" ;
urlConn.setRequestProperty("data", myData);

urlConn.connect();

BufferedReader in = new BufferedReader(newInputStreamReader(urlConn.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
}
in.close();
但是上面的代码返回以下错误

堆栈跟踪如下所示

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL: http://xxxxx.xxxx.xxx/xxxx/xxxx/xxxx/xxxx
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
    at dashboard.access.data.RAccess.main(RAccess.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
也许:

打开连接后会修复它吗?你说过你的api只允许post请求。你得到一个405方法是不允许的

如果没有,您可以尝试此链接,请查看第一章Java HttpURLConnection示例中的sendPost方法:

请确保以下文件中实施的安全性是正确的

AuthenticationFilter.java

AutherizationFilter.java

SecurityConstants.java

WebSecurity.java


Bcz spring security是由spring实现的,我们无法找到正确的异常位置。

因为我使用的是URLConnection,所以我不能使用setRequestMethod。但是示例中的sendPost方法非常有效。谢谢。我不确定他们现在是否需要走那么远。这个问题没有提到你提到的任何类。最好保持简单。他们应该从连接本身开始。错误消息提到不允许使用HTTP方法。所以一个好的开始应该是看看他们编写的te代码在使用URL和连接时实际做了什么?也许它是一个GET而不是一个POST。他们是不是走错了路?实际上,我假设从URL连接读取默认为GET动词。
urlConn.setRequestMethod("POST");