我对一种从 Java 代码以编程方式登录 OWA(Microsoft Outlook Web Access - 基于 Web 的电子邮件客户端)的方法感兴趣,并且只检索收件箱未读计数——我可以从收件箱网页的 HTML 源中读取此数字- 但问题来了 - 登录。
本质上,通过查看 OWA 登录页面的 HTML 源代码,我可以看到有一个 HTML 表单元素:
<form action="owaauth.dll" method="POST" name="logonForm" autocomplete="off">
由其中的按钮元素提交:
<input type="submit" class="btn" value="Log On" onclick="clkLgn()">
通过调查 clkLgn() 脚本,我发现它向文档发送了一个 cookie,因此它可能并不重要:
function clkLgn()
{
if(gbid("rdoPrvt").checked)
{
var oD=new Date();
oD.setTime(oD.getTime()+2*7*24*60*60*1000);
var sA="acc="+(gbid("chkBsc").checked?1:0);
var sL="lgn="+gbid("username").value;
document.cookie="logondata="+sA+"&"+sL+";expires="+oD.toUTCString();
}
}
基本上,我怎样才能发送这个表格?以下代码是我对这个问题的尝试,我可以建立 HTTP 连接 - 但我似乎无法发布正确的 HTTP 请求。
URL urlObject = new URL(url);
HttpURLConnection hConnection = (HttpURLConnection)urlObject.openConnection();
HttpURLConnection.setFollowRedirects(true);
hConnection.setDoOutput(true);
hConnection.setRequestMethod("POST");
PrintStream ps = new PrintStream(hConnection.getOutputStream());
ps.print("username="+username+"&password="+password);
ps.close();
hConnection.connect();
if( HttpURLConnection.HTTP_OK == hConnection.getResponseCode() )
{
InputStream is = hConnection.getInputStream();
OutputStream os = new FileOutputStream("output.html");
int data;
while((data=is.read()) != -1)
{
os.write(data);
}
is.close();
os.close();
hConnection.disconnect();
}
它只是不断返回相同的登录 HTML 页面。