使用Java的OpenDaylight Rest API [英] OpenDaylight Rest API with Java

查看:362
本文介绍了使用Java的OpenDaylight Rest API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布这个问题之前,我搜索很多,以确保如何问。

Before posting this question I search alot to be sure how to ask.

我试图连接到OpenDaylight控制器与Java,消耗由控制器给出的休息服务。我的问题是,当我发送http请求,我不能得到任何进一步比登录,我不知道如果其可能。

I am trying to connect to the OpenDaylight controller with Java, I am trying to connect by consuming the rest services given by the controller. My problem is, when I send the http request I cannot get any further than the login, I am not sure if its possible. Instead of getting the topology or other answer from the controller, I am getting the html of the login form.

此外,我不知道我是否应该像这样连接这个控制器的拓扑或其他答案。 。

Also, I am not sure if I should be connecting like this.

任何帮助/指导都非常感谢。 :)

Any help/guidance is very appreciated. :)

我创建连接的代码是:

public String getContent(String urls) throws  IOException {

    String cont="";

    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(urls);

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();

    nvps.add(new BasicNameValuePair("j_username", "username"));

    nvps.add(new BasicNameValuePair("j_password", "password"));

    httpPost.setEntity(new UrlEncodedFormEntity(nvps));

    CloseableHttpResponse response2 = httpclient.execute(httpPost);

    try {

        System.out.println(response2.getStatusLine());

        HttpEntity entity2 = response2.getEntity();

        BufferedReader reader =new BufferedReader(new InputStreamReader(entity2.getContent()));

        String line="";

        while((line=reader.readLine())!=null){
            cont+=line+"\n";
        }


    } finally {
        response2.close();

    }           

    return cont;
}

当我运行代码,这是什么打印:

When I run the code, this is what is printed:

HTTP / 1.1 200 OK

HTTP/1.1 200 OK

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OpenDaylight - Login</title>
<script src="/js/bootstrap.min.js"></script>    
<script type="text/javascript">
    less = {
        env: "production"
    };
</script>
<script src="/js/less-1.3.3.min.js"></script>
</head>
<body>
<form action="j_security_check;jsessionid=LONGID" id="form" method="post">
<div class="container">
 <div class="content">
   <div class="login-form">
     <div id="logo"></div>
       <fieldset>
         <div class="control-group">
           <input type="text" name="j_username" placeholder="Username">
         </div>
         <div class="control-group">
           <input type="password" name="j_password" placeholder="Password">
         </div>
         <button class="btn btn-primary" type="submit" value="Log In" >
        <div class="icon-login"></div> Log In</button>
       </fieldset>
   </div>
 </div>
 </div> 
 </form>
 </body>
 </html>


推荐答案

问题似乎是认证。用户名和密码必须编码为Base64。请尝试下面的示例代码,它以JSON格式获取流程详细信息。您可以尝试以相同的方式获取拓扑详细信息。

The problem seems to be with authentication. Username and password must be encoded to Base64. Please try the sample code below, which get the flow details in JSON format. You can to try getting topology details in the same way.

您可以从此处

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.codec.binary.Base64;

public class OpenDayLightUtils {

    public static void getFlowDetails() {

    String user = "admin";
    String password = "admin";
    String baseURL = "http://192.168.100.1:8080/controller/nb/v2/flowprogrammer";
    String containerName = "default";

    try {

        // Create URL = base URL + container
        URL url = new URL(baseURL + "/" + containerName);

        // Create authentication string and encode it to Base64
        String authStr = user + ":" + password;
        String encodedAuthStr = Base64.encodeBase64String(authStr.getBytes());

        // Create Http connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Set connection properties
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthStr);
        connection.setRequestProperty("Accept", "application/json");

        // Get the response from connection's inputStream
        InputStream content = (InputStream) connection.getInputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(content));
        String line = "";
        while ((line = in.readLine()) != null) {
        System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}

这篇关于使用Java的OpenDaylight Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆