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 秒) - 正常终止
请高人协助解决下,小弟万分感激!
在线等!