使用java发送json数据时获取filenotfoundexception

时间:2015-10-20 16:42:11

标签: java json

我正在尝试使用以下代码将json数据发送到Influx db:

String url = "http://xx.x.xx.xx:8086/db/monitoring/check_1113?u=root&p=root";

URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

conn.setRequestMethod("PUT");

//String userpass = "user" + ":" + "pass";
//String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
//conn.setRequestProperty ("Authorization", basicAuth);

//String data =  "{\"format\":\"json\",\"pattern\":\"#\"}";
System.out.println("Data to send: "+"[{\"name\": \"check_222\",\"columns\": [\"time\", \"sequence_number\", \"value\"],\"points\": [["+unixTime+", 1, \"122\"]]}]");
String data = "[{\"name\": \"check_333\",\"columns\": [\"time\", \"sequence_number\", \"value\"],\"points\": [["+14444444444+", 1, \"122\"]]}]";

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();

new InputStreamReader(conn.getInputStream());   

System.out.println("Data Sent");

其中xx.xx.xx.xx是部署涌入的服务器的ip,我正在使用IP。

当我使用此数据(在localhost上)进行手动卷曲时,数据将成功发送。卷曲如下:

curl -X POST -d '[{"name": "check_223","columns": ["time", "sequence_number", "value"],"points": [[1445271004000,1,70.8880519867]]}]' 'http://localhost:8086/db/monitoring/series?u=root&p=root'

但是当我运行代码通过上面共享的java程序发送数据时,我得到以下错误:

java.io.FileNotFoundException: http://xx.x.xx.xx:8086/db/monitoring/check_1113?u=root&p=root
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1834)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at com.snapdeal.hadoop.monitoring.hdfs1.App.sendJsonDataToInflux(App.java:52)
at com.snapdeal.hadoop.monitoring.hdfs1.App.main(App.java:89)
[INFO  - 2015-10-20T16:27:13.152Z] ShutdownReqHand - _handle - About to shutdown

要添加它,我使用phantomJS从网页获取数据并在JSON请求中传递该数据。但为了简单起见,我目前已对其进行了硬编码。

1 个答案:

答案 0 :(得分:0)

这应该是比较明显的。 405表示端点不支持请求的HTTP方法。您正在调用的服务不支持PUT方法。