我有两个文件,一个用户名列表和一个密码列表。我需要编写一个程序来检查每个用户名和密码列表。然后我需要去一个网站,看看它是否登录。我不太清楚如何进行比较以及如何模拟程序登录网站输入信息。你能帮我解决这个问题吗?这是一个家庭作业问题。
答案 0 :(得分:6)
无论您选择使用哪种语言,基本思路都是以编程方式模拟登录。这可以通过手动登录并查看HTTP标头,然后以编程方式发送“伪造”标头,更改用户/密码字段来完成。
大多数登录都会使用POST,并且发布POST并不是一件容易的事。如果您被允许使用外部库,则可以尝试cURL。只需设置相应的标题并查看响应即可检查您的尝试是否成功。如果没有,请尝试使用新组合。
在伪代码中:
bool simulate_login(user, password) :
request = new request(url)
request.set_method('POST')
request.set_header('name', user)
request.set_header('pass', password)
response = request.fetch_reponse()
return response.contains("Login successful")
success = []
foreach user:
foreach password:
if (simulate_login(user, password)):
success.append((user, password))
break
答案 1 :(得分:3)
如果您想使用java,可以尝试使用HtmlUnit(请参阅:http://htmlunit.sourceforge.net/gettingStarted.html),或者如果您被允许使用Groovy,则可以使用http://www.gebish.org/
以下是与您的案例相关的入门指南示例:
public void login() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://some_url");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlTextInput textField = form.getInputByName("userid");
// Change the value of the text field
textField.setValueAttribute("username");
// Do similar for password and that's all
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}
答案 2 :(得分:0)
如果您想使用Java,则可以尝试使用HtmlUnit(请参阅:http://htmlunit.sourceforge.net/gettingStarted.html),或者如果被允许使用Groovy,则可以使用http://www.gebish.org/
以下是与您的案例相关的入门指南中的示例:
public void login()引发异常{ 最终的WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://some_url");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlTextInput textField = form.getInputByName("userid");
// Change the value of the text field
textField.setValueAttribute("username");
// Do similar for password and that's all
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();