ITeye博客

HttpClient 登录网页

mjbb · 2010-10-25 21:26 · 编程语言 · Markdown

public class FormLoginDemo {
	static final String LOGON_SITE = "xxx.xxx.xxx.xxx";
	static final int LOGON_PORT = 8080;

	public static void main(String[] args) throws Exception {
		HttpClient client = new HttpClient();
//		client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
		
		client.getHostConfiguration().setProxy("10.99.20.36", 8080);
//		PostMethod post = new PostMethod("http://xxx.xxx.xxx:8080/hudson/j_acegi_security_check");
		PostMethod post = new PostMethod("http://container.rd.francetelecom.com/login.php");
//		 NameValuePair name = new NameValuePair( "j_username" , "xxxx" );
//	     NameValuePair pass = new NameValuePair( "j_password" , "xxxxx" );
		NameValuePair name = new NameValuePair("username", "xinru");
		NameValuePair pass = new NameValuePair("password", "adminleng");
		post.setRequestBody(new NameValuePair[] { name, pass });
		int status = client.executeMethod(post);
		post.releaseConnection();
		
//		GetMethod get = new GetMethod("/xx/xxx/ohsmain/ws/xx/xxx-xx.apk");
		GetMethod get = new GetMethod("http://container.rd.francetelecom.com/my_view_page.php");
		client.executeMethod(get);
		
//		InputStream in = get.getResponseBodyAsStream();
		InputStream  in = get.getResponseBodyAsStream();
		write2File(in);
		get.releaseConnection();
	}
	
	public static void write2File(String msg) throws Exception{
		File file = new File("c:/a.html");
		OutputStream os = new FileOutputStream(file);
		os.write(msg.getBytes());
		os.close();
	}
	
	public static void write2File(InputStream in) throws Exception{
//		File file = new File("c:/a.apk");
		File file = new File("c:/a.html");
		OutputStream os = new FileOutputStream(file);
		byte[] buff = new byte[4096];
		int len=-1;
		while((len=in.read(buff))!=-1){
			os.write(buff,0,len);
		}
		in.close();
		os.close();
	}
	
	public static InputStream getApkStream(String path){
		
		return null;
	}
}

参考资料:http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。