java模拟登陆

本程序演示请求登陆系统后,继续通过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();
		}
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值