JAVA模拟登录内网某个网页,一直接无法成功,求助!

cds1 2010-11-24 10:27:47
1.网页源文件代码如下:
<body>

<form name="form1" method="post" action="login.aspx?ReturnUrl=%2f" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEyMDc3OTEzMTYPZBYEAgMPFgIeB1Zpc2libGVoZAIFD2QWBmYPFgIfAGgWBAIBDw8WAh4EVGV4dAUt6Lqr=" />
</div>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAL1s7SnCAKpwK/OBgK1qbSRCwKC3IeGDA==" />
</div>
<div id="box02">
</div>

<div style="width: 1034px; border: 1px #ffffff solid; height: 232px; visibility: hidden;">
</div>
<div class="mainContent">
<div class="box01">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="30px" class="InputTb" style="padding-left: 70px;">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td class="ico_userid">
    
</td>
<td style="width: 65px;">
 用户名:
</td>
<td>
<input name="txtLoginName" type="text" value="fdsfs" id="txtLoginName" class="InputTextBox"

style="width:150px;" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="30px" class="InputTb" style="padding-left: 70px;">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td class="ico_password">
    
</td>
<td style="width: 65px;">
 密   码:
</td>
<td>
<input name="txtPassword" type="password" id="txtPassword" class="InputTextBox"
style="width:150px;" />
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td height="40px" style="color: #FFFFFF; padding-left: 165px; text-align: left;">
<input type="submit" name="btnLogin" value=" 登 录 " id="btnLogin" class="button" />
<input name="btReset" type="reset" id="btReset" value=" 重 置 " class="button" />
<br />
</td>
</tr>

<tr>
<td class="updatePanelBG">
<iframe scrolling="no" src="http://super100.gkmc.net:9000/webpart_support/UpdateItemList/Upd

ateItemListIframe.aspx?bgcolor=%236FB6E2&fontcolor=white"
frameborder="0" height="100" width="265"></iframe>
</td>
</tr>
</table>
</div>
</div>
<div id="test" style="display:none;">ref:http://super100.gkmc.net/_layouts/

super103/login.aspx?ReturnUrl=%2f</div>
<script type="text/javascript">
var floatReload = function(){
var maxHeight = document.getElementsByTagName("HTML")[0].offsetHeight;
var maxWidth = document.getElementsByTagName("HTML")[0].offsetWidth;
fBox = document.getElementById("box02");
var marginTop = maxHeight - fBox.offsetHeight;
fBox.style.marginTop = (marginTop > 340)?marginTop + "px":"340px"

var messageBG = document.getElementById("messageBG");
if(messageBG){
messageBG.style.height = maxHeight;
messageBG.style.width = maxWidth;
}
}
//window.onresize = floatReload;
//floatReload()
//设置透明
new (function(){
var messageBG = document.getElementById("messageBG");
if(messageBG){
var opacityPercent = 80;
messageBG.style.filter = "alpha(opacity="+opacityPercent+")";
messageBG.style.opacity = opacityPercent / 100;
}
})();
</script>

</form>
</body>



2.以下是我写的JAVA代码:


//package demo;







import org.apache.commons.httpclient.*;



import org.apache.commons.httpclient.cookie.*;



import org.apache.commons.httpclient.methods.*;







/**


* 用来演示登录表单


*/



public class FormLoginDemo2 {







static final String LOGON_SITE = "10.243.167.3";



static final int LOGON_PORT = 80;







public static void main(String[] args) throws Exception{



HttpClient client = new HttpClient();

client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);







//模拟登录页面


PostMethod post = new PostMethod("/login.aspx?ReturnUrl=%2f");

NameValuePair STATE = new NameValuePair("__VIEWSTATE", "/wEPDwULLTEyMDc3OTEzMTYPZBYEAgMPFgIeB1Zpc2libGVoZAIFD2QWBmYPFg

IfAGgWBAIBDw8WAh4EVGV4dAUt6Lqr=");

NameValuePair ATION = new NameValuePair("__EVENTVALIDATION", "/wEWBAKA1rmsBgKpwK/OBgK1qbSRCwKC3IeGDA==");

NameValuePair name = new NameValuePair("txtLoginName", "nature");
NameValuePair pass = new NameValuePair("txtPassword", "12345678");

post.setRequestBody(new NameValuePair[]{STATE,ATION,name,pass});



int status = client.executeMethod(post);



System.out.println(post.getResponseBodyAsString());



post.releaseConnection();







//查看cookie信息



CookieSpec cookiespec = CookiePolicy.getDefaultSpec();



Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/",

false, client.getState().getCookies());



if (cookies.length == 0) {



System.out.println("None");



} else {



for (int i = 0; i < cookies.length; i++) {



System.out.println(cookies[i].toString());



}



}





}



}




3.程序编译没有问题,用户名密码没有问题,但是执行的时候出现以下错误提示:
---------- java ----------
2010-11-24 10:12:15 org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
信息: ntlm authentication scheme selected
2010-11-24 10:12:15 org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
信息: No credentials available for NTLM <any realm>@10.243.167.6:80
<html>
<head>
<title>访问被拒绝。</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>

<body bgcolor="white">

<span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

<h2> <i>访问被拒绝。</i> </h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> 说明: </b>访问服务此请求所需的资源时出错。服务器可能未配置为访问所请求的 URL。
<br><br>

<b> 错误消息 401.2。: </b>未经授权: 服务器配置导致登录失败。请验证

您是否有权基于您提供的凭据和 Web 服务器上启用的身份验证方法查看此目录或页。请与

Web 服务器的管理员联系以获得其他帮助。<br><br>

<hr width=100% size=1 color=silver>

<b>版本信息:</b> Microsoft .NET Framework 版本:2.0.50727.3082; ASP.

NET 版本:2.0.50727.3082

</font>

</body>
</html>

None

输出完成 (耗时: 0 秒) - 正常终止



请高人协助解决下,小弟万分感激!
在线等!
...全文
551 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Daniel-C 2011-09-29
  • 打赏
  • 举报
回复
你应该先用Getmethod方法登陆到这个网站上,然后用读取response信息,然后分析response获得hidden域的名字和值,然后加入到你post的参数中,提交就可以登陆了.
但是我在执行一个类似的网站的时候,post登陆成功了,但是后续的redirect失败,你如果解决了,麻烦根我分享一下,谢谢.
cds1 2010-11-29
  • 打赏
  • 举报
回复
呵,没办法跟开发者接触,这本身就是想利用程序做个自动回复,不能让他们知道的

value="/wEPDwULLTEyMDc3OTEzMTYPZBYEAgMPFgIeB1Zpc2libGVoZAIFD2QWBmYPFgIfAGgWBAIBDw8WAh4EVGV4dAUt6Lqr="

value="/wEWBAL1s7SnCAKpwK/OBgK1qbSRCwKC3IeGDA=="


上面这两个的确是会变化的,如果能够实时抓取它们的值 就好了
fsdfdsffffff 2010-11-25
  • 打赏
  • 举报
回复
value="/wEPDwULLTEyMDc3OTEzMTYPZBYEAgMPFgIeB1Zpc2libGVoZAIFD2QWBmYPFgIfAGgWBAIBDw8WAh4EVGV4dAUt6Lqr="

value="/wEWBAL1s7SnCAKpwK/OBgK1qbSRCwKC3IeGDA=="

这2个东东直接复制可能不行的,你可以看看每次打开那个登陆页面时不是一样的,如果不一样就不能以这种方式解决

建议跟那个内网系统的开发者接触,了解其相关验证机制
cds1 2010-11-25
  • 打赏
  • 举报
回复
跪求高人啊
cds1 2010-11-24
  • 打赏
  • 举报
回复
麻烦指点下哦,不知道要怎么改
yaoweijq 2010-11-24
  • 打赏
  • 举报
回复
粗略看了下,
估计不能这么写,
这个网页肯定是某框架的mvc自动生成的
你程序里面的nameandpair是变动的而非固定
所以弄成那个不行。。。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧