如何调用hmc的REST接口

王庭 发布于 2015/08/04 18:26
阅读 719
收藏 0

ibm的hmc 硬件管理控制台,可以操作服务器.

hmc也开通了rest接口, 在linux上使用curl方式可以调用接口成功, [http://www.djouxtech.net/posts/hmc-v8-rest-api-part-1-curl/]

问题是, 如何使用java调用? 有人知道么

 

加载中
0
二的基本算合格
二的基本算合格

http协议的rest接口,在java中可以用HttpClient(http://hc.apache.org/index.html)调用。

下面是官网的例子(http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html):

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}

HttpPost httpPost = new HttpPost("http://targethost/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);

try {
    System.out.println(response2.getStatusLine());
    HttpEntity entity2 = response2.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity2);
} finally {
    response2.close();
}



0
王庭
王庭

引用来自“二的基本算合格”的评论

http协议的rest接口,在java中可以用HttpClient(http://hc.apache.org/index.html)调用。

下面是官网的例子(http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html):

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}

HttpPost httpPost = new HttpPost("http://targethost/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);

try {
    System.out.println(response2.getStatusLine());
    HttpEntity entity2 = response2.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity2);
} finally {
    response2.close();
}



谢谢你的回答.
httpclient的方法, 几种调用方式都试了, HttpUrlConnection方法也试了,都不行.
个人感觉hmc的rest接口和自己用restlet等工具做的接口有一些区别,具体在哪就不知道了
二的基本算合格
二的基本算合格
rest接口是一种应用层的非标准定义,不影响http协议本身。你使用HttpClient调用的是标准HTTP协议的请求/响应数据包,怎么处理选择在你,不应该出现不行的情况,把整个数据包的内容打印出来,或者抓一下包看看具体问题出在那里。
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部