本程序演示请求登陆系统后,继续通过post访问权限内的网页。
需要引入:commons-io-2.1.jar
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.apache.commons.io.IOUtils;
public class WdAdapter {
private static WdAdapter wdAdapter;
private WdAdapter(){}
public static WdAdapter getInctance(){
if(wdAdapter==null){
wdAdapter = new WdAdapter();
}
return wdAdapter;
}
private String login(){
String loginUrl = "http://218.22.6.150/csl/login";
String username = "administrator";
String password = "123456";
String cookieVal = null;
HttpURLConnection connection;
OutputStreamWriter out = null;
try {
URL url = new URL(loginUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
out = new OutputStreamWriter(connection.getOutputStream(), "GBK");
out.write("username=" + username + "&userpwd=" + password);
out.flush();
cookieVal = connection.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return cookieVal;
}
public String getInoutLog(String empUid,String sdate, String edate){
String s = "http://218.22.6.150/csl/query?action=run&uid=" + empUid + "&sdate=" + sdate + "&edate=" + edate;
HttpURLConnection resumeConnection = null;
BufferedReader bufferedReader = null;
String html = null;
try {
URL url = new URL(s);
resumeConnection = (HttpURLConnection) url.openConnection();
String cookieVal = this.login();
if (cookieVal != null) {
resumeConnection.setRequestProperty("Cookie", cookieVal);
}
resumeConnection.connect();
InputStream urlStream = resumeConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(urlStream,"GBK"));
String ss = null;
String total = "";
while ((ss = bufferedReader.readLine()) != null) {
total += ss;
}
html = new String(total.getBytes(),"utf-8");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bufferedReader!=null){
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
resumeConnection.disconnect();
}
return html;
}
public String getInoutLog(String empUid,Date sdate, Date edate){
String pattern = "";
return this.getInoutLog(empUid, DateFormat.toString(sdate, pattern), DateFormat.toString(edate, pattern));
}
public static void main(String[] args) {
try {
String newStr = new String(WdAdapter.getInctance().getInoutLog("85", "2012-02-01", "2012-02-08").getBytes(),"utf-8");
System.out.println(newStr);
IOUtils.write(newStr, new FileOutputStream("d:/index.html"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}