Java模拟登录WEB系统的简单示例

41 篇文章 1 订阅
package cwebs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/*
 * 模拟登录系统的简单示例
 * */

public class LoginSample {
	static String sURL="http://localhost:8080/LoginSample/";
	static String responseCookie;//标示Session必须
	
	//测试登录功能,返回“自动”登录后的页面
	public static String login(String usr,String pwd) throws IOException
	{
		StringBuilder sbR = new StringBuilder();
		
		//访问URL,并把信息存入sb中
		//如果服务端登录成功后,服务端的代码调用下面的代码 
		//response.sendRedirect("welcome.jsp");
		//则会不成功,原因(Step2,没有上传jsessionid值,导致没session)如下
		//Step1[login.jsp登录成功]->转到->
		//Step2[welcome.jsp不能得到session,判断没有登录成功]->转到->Step3[login.jsp要求用户登录]
		
		URL url = new URL(sURL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoInput(true);
		connection.setDoOutput(true);//允许连接提交信息		
		connection.setRequestMethod("POST");//网页默认“GET”提交方式

		StringBuffer sb = new StringBuffer();
		sb.append("Name="+usr);
		sb.append("&Password="+pwd);
		connection.setRequestProperty("Content-Length", 
				String.valueOf(sb.toString().length()));   
		
		OutputStream os = connection.getOutputStream();
		os.write(sb.toString().getBytes());
		os.close();

		//取Cookie
		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie
		System.out.println("cookie:" + responseCookie);
		
		//取返回的页面
		String line = br.readLine();
		while (line != null) {
			sbR.append(line);
			line = br.readLine();
		}
		
		return sbR.toString();
	}
	
	//返回页面
	public static String viewPage() throws IOException
	{
		StringBuilder sbR = new StringBuilder();
		
		//打开URL连接
		URL url1 = new URL(sURL);
		HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
		
		//给服务器送登录后的cookie
		connection1.setRequestProperty("Cookie", responseCookie);
		
		//读取返回的页面信息到br1
		BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));

		//取返回的页面,br1转sbR
		String line1= br1.readLine();
		while (line1 != null) {
			sbR.append(line1);
			line1 = br1.readLine();
		}
		
		return sbR.toString();	
	}	
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值