最近搭建好openstack环境,测试API的时候出错,是404错误,涉及到的代码如下:
public static Map<String, Object> get(URL url, String token) throws IOException {
HttpURLConnection conn = getURLConnection(url);
// conn.setRequestProperty("Content-Type", APPLICATION_JSON);
conn.setRequestMethod(HTTP_GET);
conn.setDoInput(true);
// conn.setDoOutput(true);
if (token != null) {
System.out.println(token);
conn.setRequestProperty("X-Auth-Token", token);
}
System.out.println(conn.toString()); if (conn.getResponseCode() == 401) {
throw new IOException("Authorize Error occurs and the response code is :" + conn.getResponseCode() + ",and the message is:" + conn.getResponseMessage());
}
System.out.println(conn.getResponseCode());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// System.out.println(mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {}));
return mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {
});
} /**
* Creates the HTTP URL connection
*
* @param url
* the URL to be used to establish HTTP connection
* @return A HttpURLConnection
*/
private static HttpURLConnection getURLConnection(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// disable cache
conn.setUseCaches(false);
// turn off HTTP keep-alive
conn.setRequestProperty("Connection", "close"); return conn;
} 上面的参数URL 是:http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
报错如下:
Exception in thread "main" java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1664)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1662)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1660)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:148)
at com.intel.cpa.openstack.NovaAPI.listServers(NovaAPI.java:46)
at com.intel.cpa.openstack.NovaAPI.main(NovaAPI.java:37)
Caused by: java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1613)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:137)
... 2 more
请问,这个是什么问题?有没有什么好的建议?
最终要的是发现404问题后思路应该什么样子的?
譬如这个,首先应该考虑构建的URL是否正确,然后这里还有一个要求是把一个值作为header传进去,也做了,请大家帮忙分析一下。
感谢您花时间思考!
public static Map<String, Object> get(URL url, String token) throws IOException {
HttpURLConnection conn = getURLConnection(url);
// conn.setRequestProperty("Content-Type", APPLICATION_JSON);
conn.setRequestMethod(HTTP_GET);
conn.setDoInput(true);
// conn.setDoOutput(true);
if (token != null) {
System.out.println(token);
conn.setRequestProperty("X-Auth-Token", token);
}
System.out.println(conn.toString()); if (conn.getResponseCode() == 401) {
throw new IOException("Authorize Error occurs and the response code is :" + conn.getResponseCode() + ",and the message is:" + conn.getResponseMessage());
}
System.out.println(conn.getResponseCode());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// System.out.println(mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {}));
return mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {
});
} /**
* Creates the HTTP URL connection
*
* @param url
* the URL to be used to establish HTTP connection
* @return A HttpURLConnection
*/
private static HttpURLConnection getURLConnection(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// disable cache
conn.setUseCaches(false);
// turn off HTTP keep-alive
conn.setRequestProperty("Connection", "close"); return conn;
} 上面的参数URL 是:http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
报错如下:
Exception in thread "main" java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1664)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1662)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1660)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:148)
at com.intel.cpa.openstack.NovaAPI.listServers(NovaAPI.java:46)
at com.intel.cpa.openstack.NovaAPI.main(NovaAPI.java:37)
Caused by: java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1613)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:137)
... 2 more
请问,这个是什么问题?有没有什么好的建议?
最终要的是发现404问题后思路应该什么样子的?
譬如这个,首先应该考虑构建的URL是否正确,然后这里还有一个要求是把一个值作为header传进去,也做了,请大家帮忙分析一下。
感谢您花时间思考!
解决方案 »
- image upload problems
- vmware10 安装了ubuntu12.04 在上面准备安装kvm 但是kvm-ok察看提示错误
- 关于域名解析所需时间
- 关于DNS的几个问题
- Hadoop运行程序卡在map 0% reduce 0%
- 阿里云云服务器部署企业级web应用的优势与不足?
- 单机运行速度比集群运行速度快,求解释这个这个应用运行在集群运行这么耗时间的原因
- docker 资料大家来分享
- Spark 学习如何入门?
- 为何HyperV和ESXi每当虚拟机启动,母机磁盘空间就会减少?虚拟内存理应是存入在虚拟机里啊
- 中国开发人员如何加入美国Openstack项目
- IaaS环境中是否可以构建分布式应用?
确认一下你的v2/xxx/servers/detail文件是否存在