问题描述
我正在尝试从网站上下载一系列CSV。目前,我一直无法正确进行身份验证。我已经尝试过使用HTMLUnit和Java.net HTTPClient。
我可以使用两种方法(HTMLUnit和HTTPClient)输入用户名和密码。但似乎我尚未登录。(我得到了页面标题,并且仍然是登录页面。)
我的问题如下。我真的只需要一个答案,它们是解决同一问题的所有不同方式
通过网页进行身份验证并保持身份验证的好方法是什么?
我不知道该网站使用了什么技术,我必须谷歌HTMLUnit Cookies,HtmlUnit会话,HtmlUnit身份验证还是?
我应该如何使用HTMLUnit对网页进行身份验证并保持身份验证?
非常感谢。我欠你5美元。
网站代码:
<!-- Latest compiled and minified CSS -->
<LINK href="/css/Signin.css" type=text/css rel=stylesheet>
<script language='javascript'> window.sessionStorage.clear();</script>
<html>
<head>
<Meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,shrink-to-fit=no">
<Meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<Meta http-equiv="expires" content="0">
<title>spin - Sign In</title>
<link href="/style.asp" type="text/css" rel="stylesheet">
<script type="text/javascript" src="/shared/scripts/3rdParty/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/shared/scripts/3rdParty/bootstrap-4.1.1/bootstrap.min.js"></script>
<script type="text/javascript" src="/shared/scripts/cookieConsent.js?v=2"></script>
<script language="JavaScript">
var ath; ath = {};
var isMobile = function() {
return /(iphone|ipod|(android.*mobile)|blackBerry|windows ce|palm|symbian|nexus 7|xoom|windows phone)/i.test(navigator.userAgent);
};
var isIPad = function(){
return /(ipad)/i.test(navigator.userAgent);
}
function CheckSavePassword() {
if (document.loginform.SavePassword.checked) {
document.loginform.SavePassword.checked = false;
} else {
document.loginform.SavePassword.checked = true;
}
}
function parseQueryString(queryString) {
var QueryString = {};
queryString = queryString.slice(queryString.indexOf("?") + 1);
var qsArray = queryString.split("&");
for (var i = 0; i < qsArray.length; i++) {
var arr = qsArray[i].split("=");
QueryString[arr[0]] = arr[1];
}
return QueryString;
}
if (window != top) {
top.location.href = location.href;
}
</script>
<style>
body {
padding:0;
}
INPUT.login {
height: 22px;
border: 1px solid #808080;
padding: 2px 4px;
background-image: url('images/bg_input.gif');
}
.mobile {
padding: 6px;
align-content: center;
align-self: center;
width: 90%;
height: auto;
display: none;
text-align: center;
border:4px solid #E7E7E7;
border-radius: 15px;
color:#444;
margin: auto;
background: -moz-linear-gradient(top,#FFFFFF,#E7E7E7);
background: -ms-linear-gradient(#FFFFFF,#E7E7E7);
background: -webkit-gradient(linear,left top,left bottom,from(#FFFFFF),to(#E7E7E7));
max-width: 25rem;
}
.mobileButton {
font-weight:bold;
border-top: 1px outset grey;
border-left: 1px outset grey;
-moz-border-radius:10px 10px 10px 10px;
-webkit-border-radius:10px 10px 10px 10px;
border-radius:10px 10px 10px 10px;
-webkit-appearance: none;
-moz-appearance:none;
min-height:2rem;
/*width:90px;
height:54px;
font-family:Arial;
font-size:x-large;*/
}
.yes{ background-color:#FBAF41;}
.no {
background-color:#BEBEBE;
}
.mobileCheckTable {
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif !important;
}
.mobileCheckTable td
{
vertical-align:top;
}
.textContent {
font-size: .9rem;
text-align:center;
font-weight:500;
}
.textContent1Child
{
white-space:Nowrap;
padding-left: .5rem;
}
.mobilecheck {
height: 1.2rem;
width: 1.2rem;
vertical-align: top;
padding: 0;
border: 1px solid rgba(0,0.3);
}
.mobilequest
{
margin:0 auto;
max-width:20rem;
Box-sizing:border-Box;
float: left;
padding-left: .5rem;
padding-bottom: .5rem;
}
div#remember {
font-size:0.88rem;
}
.bootstrap-font{
font-size:1rem;
font-family:-apple-system,sans-serif;
}
.sm-remember {
display: none;
}
@media only screen and (min-width: 476px) {
.mobile {
padding: 15px;
}
.sm-remember {
display: initial;
}
.mobilequest {
padding-bottom: 1rem;
}
.mobilequest,.textContent1Child
{
padding-left: 1rem;
}
}
@media only screen and (min-width: 768px) {
.mobilequest.textContent {
font-size: 1.5rem !important;
}
.textContent1Child {
font-size: 1.2rem !important;
}
}
@media only screen and (min-width: 1024px) {
.mobilequest.textContent {
font-size: 1.6rem !important;
}
.textContent1Child {
font-size: 1.2rem !important;
}
}
</style>
<link rel="icon" href="images/apple-touch-icon-120x120.png" type="image/x-icon" />
<link rel="shortcut icon" href="images/apple-touch-icon-120x120.png" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/addtohomescreen.css">
<script src="Scripts/addtohomescreen.js"></script>
<link rel="apple-touch-icon-precomposed" href="images/apple-touch-icon-120x120.png" type="image/x-icon" sizes="120x120"/>
<link rel="apple-touch-icon-precomposed" href="images/apple-touch-icon-152x152.png" type="image/x-icon" sizes="152x152"/>
<link rel="shortcut icon" href="images/apple-touch-icon-152x152.png" type="image/x-icon" sizes="152x152"/>
<link rel="apple-touch-icon-precomposed" href="images/apple-touch-icon-57x57.png" type="image/x-icon" sizes="57x57"/>
</head>
<body marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" >
<table height="100%" width="100%" cellpadding="0" cellspacing="0" border="0" >
<tr>
<td width="100%" height="100%" >
<form name="loginform" id="loginForm" method="POST" action="signin.asp" style="margin: 0px" onsubmit="return signInFn.loginFormOnSubmit();">
<input name="yqfzleptvalidptmzwoer" value="DT: 8/13/2020 3:11:22 PM" type="hidden">
<input name="Page_Loaded" value="DT: 8/13/2020 3:11:24 PM" type="hidden">
<table border="0" cellspacing="0" cellpadding="0" class="login">
<tr>
<td ><img src="images/SPIN_logo.jpg" width="240" height="60" /> </td>
</tr>
<tr>
<td class="text-left pl-4"><span class="h4">Sign In to</span>
<img src="images/spin-logo.jpg" width="90" height="30" /></td>
</tr>
<tr>
<td class="pl-2">
<input class="form-control" type="text" style="width: 220px;" name="user_name" maxlength="8"
value="" placeholder="Enter Your Agent ID"></td>
</tr>
<tr>
<td class="pl-2" >
<input class="form-control" type="password" style="width: 220px" name="pass" maxlength="20"
placeholder="Password">
</td>
</tr>
<tr>
<td class="text-left pl-2">
<a href="/Spin/auth/password/forgotpassword">Forgot your password?</a>
</td>
</tr>
<tr>
<td class="text-left pl-2 login-rem-me">
<input type="checkBox" name="SavePassword" value="Y" ><span onclick="CheckSavePassword();"> Remember My Password</span></td>
</tr>
<tr>
<td>
<button class="btn btn-sm btn-primary" type="submit">Sign In</button>
<!--<input class="btn btn-sm btn-primary" type="submit" value="Sign In" name="signin"></td>-->
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td>
<footer class="site-footer">
<div class="footer-content">
<div class="footer-icon SPINlogo mr-1"></div>
<div class="mb-1">© <span>SPIN Network,Inc.</span></div>
<div class="vert-bar">|</div>
<div>
804 Something Turnpike,Fakeville,State 00000
</div>
<div class="vert-bar">|</div>
<div>
800-000-3000
</div>
<div class="vert-bar">|</div>
<div class="footer-content-group">
<div class="d-inline">
<a href="http://www.spin.com/downloads/Website.pdf" target="_blank">Notice</a>
</div>
<div class="vert-bar d-inline">|</div>
<div class="d-inline">
<a href="http://www.spin.com/privacy_policy.aspx" target="_blank">Privacy Policy</a>
</div>
<div class="vert-bar d-inline">|</div>
<div class="d-inline">
<a href="http://www.spin.com/copyright_policy.aspx" target="_blank">copyright Policy</a>
</div>
</div>
<div class="vert-bar">|</div>
<div class="footer-user-count">420 users online Now.</div>
</div>
</footer>
</td>
</tr>
</table>
<div id="cookieConsentBootstrapModal" class="modal bootstrap-font" role="dialog"
aria-labelledby="cookieConsentTitle"
aria-describedby="cookieConsentDesc" aria-hidden="true" data-backdrop="static" tabindex="-1">
<div class="modal-dialog" role="document">
<!-- Modal content-->
<div class="modal-content modal-bgcolor">
<div class="modal-header">
<h1 class="modal-title h5" id="cookieConsentTitle">This website uses cookies</h1>
</div>
<div class="modal-body" id="cookieConsentDesc">
This website uses cookies for a number of purposes,including to enhance your browsing experience.
Learn more about our use of cookies in our <a href="https://www.spin.com/privacy_policy.aspx" target="_blank">Privacy Policy</a>.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-sm btn-primary js-cookie-consent-action" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
<script language="JavaScript">
document.loginform.user_name.focus();
</script>
<script>
if (!window.location.hash.match('ath'))
{
addToHome = addToHomescreen({
detectHomescreen: true,autostart: false,icon: true,lifespan: 15,maxdisplayCount: 1,appID: 'com.spin.MobileWeb'
});
addToHome.show();
window.location.hash = '#ath';
}
</script>
<script type="text/javascript">
var signInFn = (function(){
var _suppressCookieConsent = false;
function _loginFormOnSubmit(){
var isValid = true;
if (!_suppressCookieConsent && !cookieConsentUtil.cookieConsentExists()) {
isValid = false;
cookieConsentUtil.showCookieBanner();
}
_enabledisableLoginForm();
return isValid;
}
function _enabledisableLoginForm(){
if (!_suppressCookieConsent && !cookieConsentUtil.cookieConsentExists()) {
document.getElementById("loginForm").action="signin.asp";
}
else{
document.getElementById("loginForm").action="validate_new.asp";
}
}
function _focusOnFirstElement(){
try{
var focusable = $('button:visible,a[href]:visible,input:visible,select:visible,textarea:visible,[tabindex]:visible:not([tabindex="-1"])');
if (focusable.length>0){
var firstFocusable = focusable[0];
firstFocusable.focus();
}
}
catch(ex){}
}
function _docOnReady(){
//_focusOnFirstElement();
cookieConsentUtil.init({
onStoreCookieConsent: function(){
_enabledisableLoginForm();
setTimeout(_focusOnFirstElement,0);
}
});
cookieConsentUtil.docOnReady();
_enabledisableLoginForm();
}
return {
loginFormOnSubmit:_loginFormOnSubmit,docOnReady:_docOnReady
};
}());
$(document).ready(function() {
signInFn.docOnReady();
});
</script>
我的HTMLUnit代码:(再次感谢您)
public class Main {
static WebClient webClient;
@Before
public static void init() throws Exception {
webClient = new WebClient();
}
@After
public static void close() throws Exception {
webClient.close();
}
public static void signIn() throws Exception {
//Acquire location for URI,password,username,submitbutton
HtmlPage page1 = webClient.getPage("https://spin.com/signin.asp");
HtmlElement password = (HtmlElement) page1.getByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[4]/td/input").get(0);
HtmlElement username = (HtmlElement) page1.getByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[3]/td/input").get(0);
HtmlButton submitButton = (HtmlButton) page1.getByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[7]/td/button").get(0);
//Enter username,click sign in
password.type("admin");
username.type("pass");
final HtmlPage page2 = submitButton.click();
System.out.println("Postsubmit title : \n" + page2.getTitleText());
}
@Test
public static void givenAClient_gatherInfo()
throws Exception {
webClient.getoptions().setJavaScriptEnabled(false);
webClient.getoptions().setThrowExceptionOnScriptError(false);
webClient.getoptions().setCssEnabled(false);
webClient.getoptions().setRedirectEnabled(true);
HtmlPage page = webClient.getPage("https://.spin.com/signin.asp");
Assert.assertEquals(
"spin - Sign In",page.getTitleText());
System.out.println("HTMLUNIT Passtext : \n" + page.getByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[4]/td/input").get(0));
System.out.println("HTMLUNIT UserText : \n" + page.getByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[3]/td/input").get(0));
}
public static void main(String[] args) throws Exception {
init();
givenAClient_gatherInfo();
close();
signIn();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)