War*_*lax 3 html javascript java dom
我的目标是连接到OWA页面(Microsoft Office Outlook Web Access - 基本上是电子邮件客户端)并登录,然后读取加载的新页面并查找收件箱计数.
要登录,我需要填写用户名和密码字段,并调用我知道名称和标题的某个javascript函数.
我如何能:
到目前为止,我可以使用以下Java代码连接到网页并加载其页面源:
// open the connection to the welcome page
callback.status("Opening connection...");
URLConnection connection = null;
try
{
connection = url.openConnection();
}
catch(IOException ex)
{
throw new Exception("I/O Problem while attempting URL connection");
}
connection.setDoInput(true);
// open input stream to read website
callback.status("Opening data stream...");
InputStream input = null;
try
{
input = connection.getInputStream();
}
catch(IOException ex)
{
throw new Exception("I/O Problem while opening data stream");
}
// read website contents
callback.status("Reading site...");
String content = "";
byte[] buffer = new byte[100];
int totalBytesRead = 0;
int bytesRead = 0;
try
{
while((bytesRead = input.read(buffer)) != -1)
{
String newContent = new String(buffer, 0, bytesRead);
content += newContent;
}
}
catch(IOException ex)
{
throw new Exception("I/O Problem while reading website");
}
System.out.println(content);
Run Code Online (Sandbox Code Playgroud)
结果是整个页面源输出到控制台 - 很棒.我还试图解析页面以获取一个DOM对象,然后我可以按照它来查找我的用户名和密码字段:
XMLParserConfiguration config = new XML11DTDConfiguration();
DOMParser parser = new DOMParser(config);
InputSource inputSource = new InputSource(input);
inputSource.setByteStream(input);
try
{
parser.parse(inputSource);
}
catch(SAXParseException ex)
{
}
Document document = parser.getDocument();
visitNode(document, 0);
Run Code Online (Sandbox Code Playgroud)
但我得到一个SAXParseException :: 6:62:publicId和systemId之间需要空格.
看起来这一行应该归咎于:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Run Code Online (Sandbox Code Playgroud)
所以我可能需要以某种方式改变DOMParser的配置以使其足够宽松并"原谅"空白空间要求.
| 归档时间: |
|
| 查看次数: |
11584 次 |
| 最近记录: |