我正在为大学建立一个桌面客户端,允许他们在校园内创建所有电源的仪表板。该技术已经到位,该公司已经提供了一个网站来查看所有这些,但你只能一次查看一个非常烦人的网站。我的工作是设计一个桌面应用程序,让他们使用正确的凭据登录,然后立即显示所有内容。
我的问题是,我想要输入他们输入的任何用户名和密码并将其输入网站。以下是我在其网站上使用的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Log On</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<link href="core.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea")) {
document.forms[0].elements[i].focus();
break;
}
}
}
}
</script>
</head>
<body OnLoad="placeFocus()">
<table cellpadding="0" cellspacing="0" id="message">
<tr>
<td class="edging"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="msg">
<form method="post" action="/Forms/login1" name="HashForm1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="27%" class="dataName" noWrap><span id="langUsername">User Name</span>: </td>
<td width="73%"><input type="text" name="login_username" size="17" maxlength="64" value="" />
</td>
</tr>
<tr>
<td class="dataName" noWrap><span id="langPassword">Password</span>: </td>
<td><input type="password" name="login_password" size="17" maxlength="64" value="" />
</td>
</tr>
<tr>
<td class="dataName" noWrap><span id="langLanguage">Language</span>: </td>
<td><select name="prefLanguage" size="1">
<option value=00000000 selected="true">English
</select></td>
</tr>
<tr>
<td colspan="2" class="applyCancel"><input type="submit" name="submit" value="Log On" class="btn" />
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
我想要做的是关注这三行:
<td width="73%"><input type="text" name="login_username" size="17" maxlength="64" value="" /></td>
<td><input type="password" name="login_password" size="17" maxlength="64" value="" />
<td colspan="2" class="applyCancel"><input type="submit" name="submit" value="Log On"class="btn" />
他们是用户名登录,密码输入和提交按钮
我想要做的是使用java自动填充这两个文本域,然后自动激活提交按钮以便从网站进行验证。我的问题是,我不确定如何去做。我可以在java中使用Jsoup解析页面但是当我有这个时我该怎么办?或者我根本不使用它?
以下是我的3个课程:
主:
/**
*
* The program requires a login already defined by
* the administration. Then the program proceeds to
* retrieve information from a local IP address.
*
* This information is in the form of a webview
* to help centralize all of the data for the UPS's
*/
package resourcemonitor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import org.jsoup.Jsoup;
/**
* @author
*/
public class Main {
/**
* @param args
*
*/
private static JTextField tfUsername;
private static JPasswordField pfPassword;
private static JLabel lbUsername;
private static JLabel lbPassword;
private static JButton btnCancel;
private static boolean succeeded;
private static String address;
public static void main(String[] args) {
final JFrame frame = new JFrame("Resource Monitor");
JButton btnLogin = new JButton("Click to login");
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);
tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);
lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);
pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Login.authenticate(getUsername(), getPassword())) {
//succeeded with the login
//save the information in another class
UserInformation UI = new UserInformation();
UI.setUserName(getUsername());
UI.setPassword(getPassword());
//developer help
System.out.println(UI.getUserName());
System.out.println(UI.getPassword());
//get rid of the log in screen
frame.dispose();
//create a new frame so that you can display the dashboard.
final JFrame dash = new JFrame("DashBoard");
//by doing this, the whole view becomes scrollable
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
dash.add(scrPane);
//end of scrollable
//contents of the dash
JLabel introText = new JLabel("Welcome to the UPS (Unified Power Source) dashboard monitor\n" +
"Please select one of the IP addresses to access and click select");
//adding it to the display
JPanel intro = new JPanel();
intro.add(introText);
dash.getContentPane().add(intro, BorderLayout.PAGE_START);
final String subject[] = {"10.117.1.1", "10.117.61.1", "10.117.183.1", "10.117.122.1", "10.117.3.1", "10.117.3.2"};
final JList<Object> list = new JList<Object>(subject);
dash.add(list);
/*
* the IP addresses that I will be working with
*
* 10.117.1.1
* 10.117.61.1
* 10.117.183.1
* 10.117.122.1
* 10.117.3.1
* 10.117.3.2
*
*/
JPanel buttonUpdaterPanel = new JPanel();
final JButton select = new JButton("Select");
buttonUpdaterPanel.add(select);
dash.getContentPane().add(select, BorderLayout.PAGE_END);
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Get the ip address selected in the list
address = (String) list.getSelectedValue();
System.out.println(address);
//destroy the selection window
dash.dispose();
//create a new window with the web view
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
org.jsoup.nodes.Document doc = null;
try {
doc = Jsoup.connect("http://" + address + "/logon.htm").get();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
org.jsoup.select.Elements newsHeadlines = doc.select("tr");
System.out.println(newsHeadlines);
//URL request
try {
//whatever URL needed. Might be better just to parse
//specific <div> tags
jep.setPage("http://" + address + "/logon.htm");
}catch (IOException m) {
jep.setContentType("text/html");
jep.setText("<html>Could not load</html>");
System.out.println(m);
}
//JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jep);
f.setMinimumSize(new Dimension(800,600));
f.setVisible(true);
}
});
//size and visibility
dash.setSize(750, 400);
dash.setMinimumSize(new Dimension(400, 400));
dash.setLocationRelativeTo(null);
dash.setVisible(true);
succeeded = true;
} else {
//failed the login
//showing the error message
JOptionPane.showMessageDialog(null, "I'm sorry but I do not recognize that username/password combination\n"
+ "Please click ok or hit the enter key to try again");
// reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pfPassword.setText("");
tfUsername.setText("");
}
});
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);
//by setting the default button, hitting the
//enter key causes the button to be clicked
//and activate the onclick listener
frame.getRootPane().setDefaultButton(btnLogin);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(bp, BorderLayout.PAGE_END);
frame.setMinimumSize(new Dimension(325, 125));
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(frame);
frame.setVisible(true);
}
public static String getUsername() {
return tfUsername.getText().trim();
}
public static String getPassword() {
return new String(pfPassword.getPassword());
}
public static boolean isSucceeded() {
return succeeded;
}
}
登录:
package resourcemonitor;
public class Login {
public static boolean authenticate(String username, String password) {
// hardcoded username and password
if (username.equals("user") && password.equals("pass")) {
return true;
}
return false;
}
}
UserInformation:
package resourcemonitor;
//this is a simple handler class where
//the information of the user can be saved
//so that it can be used later
public class UserInformation {
private String name, password;
public UserInformation(){
//empty constructor
}
public String getPassword(){
return password;
}
public String getUserName(){
return name;
}
public void setUserName(String m){
this.name = m;
}
public void setPassword(String n){
this.password = n;
}
}
我从这个url
下载了jsoup我知道它真的很长,但我真的很感激任何和所有的帮助。
答案 0 :(得分:0)
您需要做的是创建一个Jsoup POST http request,其中包含表单数据作为参数。 JSoup可以做到。
Document doc = Jsoup.connect("the.url")
.data("login_username", whatever)
.data("password", thpass)
.post();
到目前为止,我对你的问题没有更深入的了解。如果您需要更多帮助,请离开。
您可能需要读出作为回复获得的Cookie,并将其设置为任何进一步的请求。
这样做的更好选择是使用完整的Apache HttpClient