在发布这个问题之前,我进行了大量搜索以确定如何提问。
我正在尝试使用 Java 连接到 OpenDaylight Controller ,我正在尝试通过使用 Controller 提供的其余服务进行连接。我的问题是,当我发送 http 请求时,除了登录之外我无法获得任何进一步的信息,我不确定是否可能。我没有从 Controller 获取拓扑或其他答案,而是获取登录表单的 html。
此外,我不确定是否应该这样连接。
非常感谢任何帮助/指导。 :)
我用于创建连接的代码是:
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;
}
当我运行代码时,打印的内容如下:
HTTP/1.1 200 正常
<!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 格式获取流程详细信息。您可以尝试以相同的方式获取拓扑详细信息。
您可以从here下载commons-codec库
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 - 使用 Java 的 OpenDaylight Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23204630/