在发布这个问题之前,我搜索了很多以确定如何提问。
我正在尝试使用 Java 连接到 OpenDaylight 控制器,我正在尝试通过使用控制器提供的其余服务进行连接。我的问题是,当我发送 http 请求时,除了登录之外我无法获得任何进一步的信息,我不确定它是否可能。我没有从控制器获取拓扑或其他答案,而是获取登录表单的 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>