【发布时间】:2016-09-17 12:07:55
【问题描述】:
我想发出 POST 请求以从 Openstack 获取令牌。 我可以通过输入 url:"http://*******/v2.0/tokens" 在 Mozilla 上使用插件来做到这一点 和数据作为
{
"auth": {
"tenantName": "admin",
"passwordCredentials": {
"username": "xxxxxx",
"password": "xxxxxx"
}
}
}
JAVA程序怎么做? 到目前为止,我已经尝试了以下代码,但没有成功。
package rest.openstack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientGet {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
URL url = new URL("http://***.**.**.**:5000/v2.0/tenants/"); //url for openstack
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
为什么不使用来自wiki.openstack.org/wiki/SDKs#Java的Java SDK之一
标签: java rest post openstack restful-authentication