.net+jquery+ashx实现客户端模拟登陆扩展
客户端实现:login
namespace LoginApp
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtPwd = new System.Windows.Forms.TextBox();
this.txtUserName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.btnClose = new System.Windows.Forms.Button();
this.btnLogin = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(, );
this.txtPwd.Margin = new System.Windows.Forms.Padding();
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(, );
this.txtPwd.TabIndex = ;
this.txtPwd.Text = "admin";
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(, );
this.txtUserName.Margin = new System.Windows.Forms.Padding();
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(, );
this.txtUserName.TabIndex = ;
this.txtUserName.Text = "admin";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Margin = new System.Windows.Forms.Padding(, , , );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "密码";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Margin = new System.Windows.Forms.Padding(, , , );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "用户名";
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(, );
this.btnClose.Margin = new System.Windows.Forms.Padding();
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(, );
this.btnClose.TabIndex = ;
this.btnClose.Text = "关闭";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(, );
this.btnLogin.Margin = new System.Windows.Forms.Padding();
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(, );
this.btnLogin.TabIndex = ;
this.btnLogin.Text = "登录";
this.btnLogin.UseVisualStyleBackColor = true;
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnLogin);
this.Name = "Form1";
this.Text = "登录";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnLogin;
}
}
后台登陆代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace LoginApp
{
public partial class Form1 : Form
{
string strURL = "http://localhost:50229/Ashx/Login.ashx";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
string name = this.txtUserName.Text.Trim();
string pwd = this.txtPwd.Text.Trim();
// string postData = name;//要提交的数据
//GetPage(strURL, postData);
//定义返回json数据 解析json数据并显示信息到窗体控件值上
// string jsoncontent = GetPage(strURL, postData); //List<Custerm> CustermList = JsonConvert.DeserializeObject<List<Custerm>>(jsoncontent); //foreach (Custerm custerm in CustermList)
//{ // this.txtName.Text = custerm.CustermName;
//} StringBuilder sb = new StringBuilder();
sb.AppendFormat("name={0}&pwd={1}", name, pwd);
string result = GetPage(strURL, sb.ToString());
switch (result)
{
case "":
MessageBox.Show("登录成功!");
break;
case "":
MessageBox.Show("登录失败!");
break;
default:
MessageBox.Show("登录参数错误!");
break;
}
} /// <summary>
/// Post数据到web服务端
/// </summary>
/// <param name="strURL">网址</param>
/// <param name="postData">参数</param>
/// <returns></returns>
public string GetPage(string strURL, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8"); byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(strURL) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
//Post请求方式
request.Method = "POST";
//是否保持常连接
request.KeepAlive = false;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
//内容类型
request.ContentType = "application/x-www-form-urlencoded";
//表示请求消息正文的长度
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
//send the data//将传输的数据,请求正文写入请求流
outstream.Write(data, , data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();//获得请求流
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
Console.WriteLine(content);
string err = string.Empty;
return content; }
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
服务端login
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系统登录</title>
<script type="text/javascript" src="Scripts/jquery-1.11.0.js"></script>
<script type="text/javascript">
$(function () {
$("#btnLogin").click(function () {
var name = $("#txtUserName").val();
var pwd = $("#txtPwd").val();
$.ajax({
url: "Ashx/Login.ashx",
data: "name=" + name + "&pwd=" + pwd,
type: "Post",
dataType: "text",
success: function (msg) {
if (msg == "1") {
$("#divMsg").html("登录成功!");
} else {
$("#divMsg").html("登录失败!");
}
} });
});
});
</script>
</head>
<body>
<div style="text-align:center;">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="txtUserName" name="name" value="admin" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" id="txtPwd" name="name" value="admin" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" id="btnLogin" name="name" value="登录" /></td>
</tr>
</table>
<div id="divMsg"></div>
</div>
</body>
</html>
一般处理程序;Login.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LoginWebApp.Ashx
{
/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//接收客户端传送过来的用户名和密码
string name = context.Request["name"];
string pwd = context.Request["pwd"];
Userinfo info = new Userinfo();
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd))
{
context.Response.Write("");
//登录失败
}
else
{
if (name == "admin" && pwd == "admin")
{
//登录成功记入cookie
context.Response.Cookies["n"].Value = name;
context.Response.Cookies["n"].Expires = DateTime.Now.AddDays();
context.Response.Cookies["p"].Value = pwd;
context.Response.Cookies["p"].Expires = DateTime.Now.AddDays();
context.Response.Write("");
//登陆成功
}
else
{
context.Response.Write("");
}
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}
引用jquery jquery-1.11.0.js以及样式 后端登录可以扩展实现数据库的访问方式获取用户名和密码判断登录
这里主要展现实现模拟登陆的方式 看了其他文章 跟以前做的有点相似所以在此做个改动 其实原理一样。
.net+jquery+ashx实现客户端模拟登陆扩展的更多相关文章
- Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题
前一段时间,在瞎看看 Node.js,便研究通过 Node.js 实现知乎模拟登陆.相信,有很多网站有登陆权限设置,如若用户未登陆,将会跳转至首页提醒用户登陆,无法浏览部分页面. 如若是 b/s 架构 ...
- (转)[jQuery]使用jQuery.Validate进行客户端验证(初级篇)——不使用微软验证控件的理由
以前在做项目的时候就有个很大心病,就是微软的验证控件,虽然微软的验证控件可以帮我们完成大部分的验证,验证也很可靠上手也很容易,但是我就是觉得不爽,主要理由有以下几点: 1.拖控件太麻烦,这个是微软控件 ...
- 通过HttpWebRequest实现模拟登陆
1>通过HttpWebRequest模拟登陆 using System; using System.Collections.Generic; using System.Linq; using S ...
- [jQuery]使用jQuery.Validate进行客户端验证(初级篇)
以前在做项目的时候就有个很大心病,就是微软的验证控件,虽然微软的验证控件可以帮我们完成大部分的验证,验证也很可靠上手也很容易,但是我就是觉得不爽,主要理由有以下几点: 1.拖控件太麻烦,这个是微软控件 ...
- 爬虫之 cookie , 验证码,模拟登陆,线程
需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- NetworkComms V3 模拟登陆
演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...
- python模拟登陆知乎并爬取数据
一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...
- 新浪微博模拟登陆+数据抓取(java实现)
模拟登陆部分实现: package token.exe; import java.math.BigInteger; import java.util.Random; import org.apache ...
随机推荐
- storm0.9.0.1升级安装
来自:http://blog.csdn.net/liuzhoulong/article/details/21112101 1,下载0.9.0.1 http://storm.incubator.apac ...
- 为什么学习Python及Python环境安装
大部分人在工作中可能是以c/c++.java之类的语言为主.这也可能是我们接触的第一个开发语言,这类语言一般有丰富地类库.高效地运行速率.灵活地组合控制,须要经过编译在运行.适用于大型的项目proje ...
- or1200中载入存储类指令说明
下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 OR1200中实现的载入存储类指令有8条,每条指令的作用与说明如表9.1所看到的. watermark/2/text/aHR0cDo ...
- Linux下监视NVIDIA的GPU使用情况(转)
在使用TensorFlow跑深度学习的时候,经常出现显存不足的情况,所以我们希望能够随时查看GPU时使用率.如果你是Nvidia的GPU,那么在命令行下,只需要一行命令就可以实现. 1. 显示当前GP ...
- 使用java爬取国家统计局的12位行政区划代码
前言: 本文基于j2ee的原始url进行都写,解析指定内容时也是使用很傻的形式去查找指定格式的字符串来实现的. 更优雅的方式是可以使用apache的HttpClient和某些文档模型将HTML字符串构 ...
- 在JQuery中获取URL中的参数值
添加一个js文件,代码如下 // * jQuery url get parameters function [获取URL的GET参数值] // *character_set UTF-8 // * au ...
- java装箱拆箱
基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我们会这样: Class a = new Class(p ...
- Centos5, 6, 以及Ubuntu18.04下更改系统时间和时区
http://www.namhuy.net/2435/how-to-change-date-time-timezone-on-centos-6.html 查看日期(使用 -R 参数会以数字显示时区) ...
- PHP-VC9/VC6 TS/NTS等版本之间的区别
PHP的更新升级是越来越快了,PHP 5.2 版本已经更新到5.2.17不再更新, 5.3版本的更新到了5.3.8,PHP 5.4马上就要发布,甚至PHP6.0也在开发中.有这么多版本供我们选择,真是 ...
- PHP-深入学习Smarty
本文中的边界标签分别为"<{"和"}>" start-12, 都是静态模板中的内容, 即使函数也只是模板中的标签或者变量调解器; 13-end, 都 ...