从Java发送OWA登录表单 [英] Sending an OWA logon form from Java
问题描述
我有兴趣以编程方式从Java代码登录到OWA(Microsoft Outlook Web Access - 基于Web的电子邮件客户端),并且只检索收件箱未读数 - 我可以从收件箱网页中读取此号码页面的HTML源代码,但问题是到达那里 - 登录。
本质上,从查看OWA登录页面的HTML源代码可以看出, HTML表单元素:
< form action =owaauth.dllmethod =POSTname =logonFormautocomplete = 关 >
由该按钮元素提交:
< input type =submitclass =btnvalue =Log Ononclick =clkLgn()>
从调查clkLgn()脚本,我发现它向文档发送一个cookie,以便它可以不是至关重要的:
function clkLgn()
{
if(gbid(rdoPrvt)。检查)
{
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)。
document.cookie =logondata =+ sA +&+ sL +; expires =+ oD.toUTCString();
}
}
基本上如何发送此表单?
以下代码是我尝试的问题,我可以使HTTP连接 - 但我似乎无法POST正确的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 +& amp; amp; password =+ password);
ps.close();
hConnection.connect();
if(HttpURLConnection.HTTP_OK == hConnection.getResponseCode())
{
InputStream is = hConnection.getInputStream();
OutputStream os = new FileOutputStream(output.html);
int数据;
while((data = is.read())!= -1)
{
os.write(data);
}
is.close();
os.close();
hConnection.disconnect();
}
它只是继续返回相同的登录HTML页面。
JavaScript确实是一件重要的事情:它向文档添加了一个cookie。需要一个合适的HTTP客户端,以在每个HTTP请求上的标头上发送所有有效的cookie。你应该以编程方式做同样的事情。您可以使用 URLConnection#setRequestProperty()
。
此外,在编程方式提交表单时,还需要考虑几件事情:您不应该跳过任何隐藏的输入字段( input type =hidden
),那些可能是相关的。您还应该发送要按程序方式按提交按钮的提交按钮的 name = value
对。最后,您不应该使用& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;请注意,我不保证最终可以工作,OWA的事情可能还有一些其他的预防机器人,但是它应该能够解决这个问题。
另请参见:
顺便说一句,你是否考虑过使用SMTP / IMAP API连接它,如 JavaMail ?
I am interested in a way to programmatically log into OWA (Microsoft Outlook Web Access - a web-based email client) from Java code and retrieve nothing more than the inbox unread count -- I can read this number from the inbox web page's HTML source - but the problem is getting there - logging in.
Essentially, from looking at the HTML source of the OWA logon page, I can see that there is an HTML form element:
<form action="owaauth.dll" method="POST" name="logonForm" autocomplete="off">
that gets submitted by a button element within it:
<input type="submit" class="btn" value="Log On" onclick="clkLgn()">
From investigating the clkLgn() script, I find that it sends a cookie to the document so it may not be crucial:
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();
}
}
Basically, how can I send this form? The following code is my attempt at the problem, I can make the HTTP connection - but I can't seem to be able to POST the correct HTTP request.
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();
}
It just keeps returning the same logon HTML page.
That JavaScript does certainly an important thing: it adds a cookie to the document. A decent HTTP client is required to send all valid cookies along the headers on every HTTP request. You should do the same programmatically. You can add headers using URLConnection#setRequestProperty()
.
Further, there are several things to take into account as well when submitting forms programmatically: you should not skip any hidden input fields (input type="hidden"
), those might be of relevance. You should also send the name=value
pair of the submit button you'd like to press programmatically along as request parameter. Finally, you should not be using &
to concatenate parameter pairs, but &
.
Note that I don't guarantee that it will finally work, that OWA thing might have some other prevention against bots, but it should solve the as far spotted problems.
See also:
By the way, have you considered just connecting it using a SMTP/IMAP API like JavaMail?
这篇关于从Java发送OWA登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!