java owa,从Java发送OWA登录表单

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:

that gets submitted by a button element within it:

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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值