应用需求:自动归档,将需要归档的文档的页面(是整个html)存为附件,放在归档后的文档中。
// 连接地址(取得联系L)
String surl = "http://oatest.fj.cmcc/names.nsf?login";
URL url = new URL(surl);
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
//其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称
out.write("UserName=zhukongrenyuan&Password=a"); // post的关键所在!
out.flush();
out.close();
connection.connect();
String cookieVal = connection.getHeaderField("Set-Cookie");
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
URL ibmURL = new URL("http://oatest.fj.cmcc/SGSOA/fwglcx.nsf/0/4825781B006DF1E5482579E6000853E6?opendocument");
HttpURLConnection resumeConnection = (HttpURLConnection) ibmURL.openConnection();
if (cookieVal != null) {
//发送cookie信息上去,以表明自己的身份,否则会被认为没有权限
resumeConnection.setRequestProperty("Cookie", cookieVal);
}
resumeConnection.connect();
InputStream urlStream = resumeConnection.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(urlStream));
//BufferedReader bin = new BufferedReader ( new InputStreamReader(ibmURL.openStream( ) ));
String line;
StringBuffer sb = new StringBuffer();
while ( (line = bin.readLine( )) != null )
{
sb.append(line);
}
String ibmString = sb.toString();
Document newNotesDoc = db.createDocument();
newNotesDoc.replaceItemValue ("Form", "IBMForm");
newNotesDoc.replaceItemValue ("WebPageUS", ibmString);
newNotesDoc.computeWithForm( true , false);
newNotesDoc.save( true, true );
newNotesDoc.recycle();
db.recycle();
} catch(Exception e) {
e.printStackTrace();
}
}
}