Director REST API 的使用

IBMSystemDirectorRESTAPI调用
本文介绍如何使用Java的HttpsURLConnection调用IBMSystemDirector的RESTAPI,涵盖HTTPS配置、请求方法(GET、POST、PUT、DELETE)及HTTP请求构造过程。

最近要使用IBM System Director的REST API,官方提供的例子上用的了HttpsURLConnection来发送GET、POST、DELETE、PUT请求。


REST架构风格是基于无状态web服务,通常使用HTTP协议访问。因此,简单的HTTP客户端是调用REST APIs最有效的工具,如poster。Java的javax.net.ssl 包提供了访问一组类(HttpsURLConnection)来访问IBM System Director的https连接。

Director的REST API的HTTP 请求方法

HTTP MethodDescription
GET

请求资源

Return information concerning the resource in the body of the HTTP response.

PUT

更新资源,如果不存在此资源,则返回404 

Update the resource if it already exists based on information passed in the body of the HTTP request. An HTTP response code of 404 will be returned if the resource doesn't exist.

POST

创建或新加资源

Create a new instance of a resource based on information passed in the body of the HTTP request.

DELETE

删除

Remove the resource.

HEAD                                   Used for POST Once Exactly                                                                                                                                                                                     


1、配置安全套接字 Secure Socket Layer

可以手动配置,官方的例子上也实现了动态生成的方法。

自动实现的方法:

   // create a temporary file that will be removed when the JVM exits (normally)
   // optionally, you can create a permanent file to hold the SSL certificate
   File keyStoreFile = File.createTempFile("tmpkeystore", ".jks"); 
   keyStoreFile.deleteOnExit();

   // retrieve the certificate and store it in the keystore file
   KeyStore ts = createKeyStore(serverName, port, keyStoreFile, certificateStorePassword);
   	  
   // set up the TrustManager and sslSocketFactory using keystore containing certificate 
   TrustManagerFactory tmf = TrustManagerFactory.getInstance("IBMX509");
   tmf.init(ts);
   TrustManager[] tm = tmf.getTrustManagers();

   SSLContext sslContext = SSLContext.getInstance("SSL");
   sslContext.init(null, tm, null);
   sslSocketFactory = sslContext.getSocketFactory();

   // disable hostname verification
   HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }	  
    };

   // create authorization token string with the UserID and password
   String userInfo  = "DirectorServer_UserID" + ":" + new String("DirectorServer_Password");
   authToken = "Basic " + Base64.encode(userInfo.getBytes());
        
   URL myURL = new URL("https://mysystem.ibm.com:8422/ibm/director/rest/resources/OperatingSystem");
   HttpsURLConnection.setDefaultHostnameVerifier(hv);
   HttpsURLConnection conn = (HttpsURLConnection)myURL.openConnection();
   conn.setSSLSocketFactory(sslSocketFactory);
   conn.setRequestProperty("Authorization", authToken);
   conn.setRequestProperty("ISDAPIVersion", VERSION);
   conn.setRequestMethod("GET");


conn.setRequestProperty( "content-type", "application/json; charset=UTF-8" ); // 进行编码
        // if any keys are specified in requestHeaders that were already set (above), they are overridden here
	//设置参数
       
    if (requestHeaders != null) {
         Set<String> keys = requestHeaders.keySet();
         for (String key : keys) {
          conn.setRequestProperty(key, requestHeaders.get(key));
         }
    }
         
   //设置请求的body
   // 设置是否向connection输出,因为这个是post请求,参数要放在
     // http正文内,因此需要设为true
   if (requestBody != null) {
            secureConnection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(secureConnection.getOutputStream());  
            out.write(requestBody);
            out.close();
   }
   
   //连接
   conn.connect();

   //获得请求返回状态码
   responseCode    = conn.getResponseCode();

   //获得请求返回内容
    BufferedReader rd  = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); //以utf-8的格式输出
        
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            if (line.trim().length() > 1) {
                sb.append(line);
            }
        }
        rd.close();
     String result = sb.toString();

这里面的conn.connect函数,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。无论是post还是get,http请求实际上直到conn.getInputStream()这个函数里面才正式发送出去。

http请求实际上由两部分组成,一个是http头,所有关于此次http请求的配置都在http头里面定义,一个是正文content,在connect()函数里面,会根据HttpURLConnection对象的配置值生成http头,因此在调用connect函数之前,就必须把所有的配置准备好。

紧接着http头的是http请求的正文,正文的内容通过outputStream写入,实际上outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,而是在流关闭后,根据输入的内容生成http正文。

至此,http请求的东西已经准备就绪。在getInputStream()函数调用的时候,就会把准备好的http请求正式发送到服务器了,然后返回一个输入流,用于读取服务器对于此次http请求的返回信息。由于http请求在getInputStream的时候已经发送出去了(包括http头和正文),因此在getInputStream()函数之后对connection对象进行设置(对http头的信息进行修改)或者写入outputStream(对正文进行修改)都是没有意义的了,执行这些操作会导致异常的发生。


参考:Http学习之使用HttpURLConnection发送post和get请求 - pandazxx的专栏 - 博客频道 - youkuaiyun.com











http://book.51cto.com/art/201203/323928.htm   http和rest   深入理解http和rest的关系

http://www.cnblogs.com/springyangwc/archive/2012/01/18/2325784.html 介绍rest  深入理解rest的实现









提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值