C#最简单的登录Web服务
2015-07-10 12:06
465 查看
由于演示程序,所以有以下问题:
1,密码是明码传输。
本文在
C#建立最简单的web服务,无需IIS 的基础上完成。
具体步骤:
一,RequestProcessor增加变量mLogin,改写ParseRequestAndProcess函数。
protected bool mLogin = false;
public bool ParseRequestAndProcess(string[] RequestLines)//解析内容
{
char[] sp = new Char[1] { ' ' };
string[] strs = RequestLines[0].Split(sp);
if (strs[0] == "GET")
{
Send(strs[1], 0, 0);
}
else if ("POST" == strs[0])
{
for (int j = 1; j < RequestLines.Length; j++)
{
if (RequestLines[j].IndexOf('&') >= 0)
{
if( mLogin )
{
SendHeadrAndStr("已经登录,不能重复登录。");
}
else
{
//分拆用户名和密码
String sName = "";
String sPassWord = "";
string[] s = RequestLines[j].Split('&');
for (int k = 0; k < s.Length; k++)
{
string[] sItems = s[k].Split('=');
if (null == sItems || sItems.Length < 2)
continue;
if ("username" == sItems[0])
sName = sItems[1];
if ("password" == sItems[0])
sPassWord = sItems[1];
}
//模拟登录
if ("a" == sName && "b" == sPassWord)
{
mLogin = true;
SendHeadrAndStr("登录成功。");
}
else
{
SendHeadrAndStr("用户名、密码不匹配。");
}
}
}
}
}
return false;
}
二,改写index.htm。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>3</title>
<script>
function check()
{
var username=document.getElementById("username");
var pass=document.getElementById("password");
if(username.value=="")
{
alert("请输入用户名");
username.focus();
return false;
}
if(pass.value=="")
{
alert("请输入密码");
pass.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name= "LoginForm" method="post" onsubmit="return check()">
<table width="350" bgcolor="#ccffcc" style="border-color" border="1">
<tr>
<td width="100">
用户名
</td>
<td>
<input type="text" name="username" id="username">
</td>
</tr>
<tr>
<td>
密 码
</td>
<td>
<input type="password" name="password" id="password">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="登 录" >
</td>
</tr>
</table>
</form>
</body>
</html>
1,密码是明码传输。
本文在
C#建立最简单的web服务,无需IIS 的基础上完成。
具体步骤:
一,RequestProcessor增加变量mLogin,改写ParseRequestAndProcess函数。
protected bool mLogin = false;
public bool ParseRequestAndProcess(string[] RequestLines)//解析内容
{
char[] sp = new Char[1] { ' ' };
string[] strs = RequestLines[0].Split(sp);
if (strs[0] == "GET")
{
Send(strs[1], 0, 0);
}
else if ("POST" == strs[0])
{
for (int j = 1; j < RequestLines.Length; j++)
{
if (RequestLines[j].IndexOf('&') >= 0)
{
if( mLogin )
{
SendHeadrAndStr("已经登录,不能重复登录。");
}
else
{
//分拆用户名和密码
String sName = "";
String sPassWord = "";
string[] s = RequestLines[j].Split('&');
for (int k = 0; k < s.Length; k++)
{
string[] sItems = s[k].Split('=');
if (null == sItems || sItems.Length < 2)
continue;
if ("username" == sItems[0])
sName = sItems[1];
if ("password" == sItems[0])
sPassWord = sItems[1];
}
//模拟登录
if ("a" == sName && "b" == sPassWord)
{
mLogin = true;
SendHeadrAndStr("登录成功。");
}
else
{
SendHeadrAndStr("用户名、密码不匹配。");
}
}
}
}
}
return false;
}
二,改写index.htm。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>3</title>
<script>
function check()
{
var username=document.getElementById("username");
var pass=document.getElementById("password");
if(username.value=="")
{
alert("请输入用户名");
username.focus();
return false;
}
if(pass.value=="")
{
alert("请输入密码");
pass.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name= "LoginForm" method="post" onsubmit="return check()">
<table width="350" bgcolor="#ccffcc" style="border-color" border="1">
<tr>
<td width="100">
用户名
</td>
<td>
<input type="text" name="username" id="username">
</td>
</tr>
<tr>
<td>
密 码
</td>
<td>
<input type="password" name="password" id="password">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="登 录" >
</td>
</tr>
</table>
</form>
</body>
</html>
相关文章推荐
- C#实现麦克风采集与播放
- C#部署安装,将用户安装路径记录下写入注册表,并启动
- C# 中使用 Obsolete 标志 代码过期
- C#的常量和变量以及其作用域和命名规范
- 【C#】程序运行时间
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
- 【C#学习杂记】Directory获取文件路径
- C#中的基本数据类型
- 浅谈C#中Md5和Sha1两种加密方式
- C#生成唯一不重复订单号
- C#注册表操作,根据键取值
- C# 目录(文件夹)复制实现
- C#分页
- C#实现bitmap图像矫正
- C#三层中的分页
- C# 编写Windows服务并设置为“允许服务与桌面交互”
- C# Parallel用法
- C#实现根据银行卡卡号判断银行名
- c# 正则匹配程序
- 5个最优秀的Java和C#代码转换工具