客户端实现: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实现客户端模拟登陆扩展的更多相关文章

  1. Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题

    前一段时间,在瞎看看 Node.js,便研究通过 Node.js 实现知乎模拟登陆.相信,有很多网站有登陆权限设置,如若用户未登陆,将会跳转至首页提醒用户登陆,无法浏览部分页面. 如若是 b/s 架构 ...

  2. (转)[jQuery]使用jQuery.Validate进行客户端验证(初级篇)——不使用微软验证控件的理由

    以前在做项目的时候就有个很大心病,就是微软的验证控件,虽然微软的验证控件可以帮我们完成大部分的验证,验证也很可靠上手也很容易,但是我就是觉得不爽,主要理由有以下几点: 1.拖控件太麻烦,这个是微软控件 ...

  3. 通过HttpWebRequest实现模拟登陆

    1>通过HttpWebRequest模拟登陆 using System; using System.Collections.Generic; using System.Linq; using S ...

  4. [jQuery]使用jQuery.Validate进行客户端验证(初级篇)

    以前在做项目的时候就有个很大心病,就是微软的验证控件,虽然微软的验证控件可以帮我们完成大部分的验证,验证也很可靠上手也很容易,但是我就是觉得不爽,主要理由有以下几点: 1.拖控件太麻烦,这个是微软控件 ...

  5. 爬虫之 cookie , 验证码,模拟登陆,线程

    需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...

  6. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  7. NetworkComms V3 模拟登陆

    演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...

  8. python模拟登陆知乎并爬取数据

    一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...

  9. 新浪微博模拟登陆+数据抓取(java实现)

    模拟登陆部分实现: package token.exe; import java.math.BigInteger; import java.util.Random; import org.apache ...

随机推荐

  1. php之phpstorm入门

    入门篇!见这章

  2. Java AES 加密工具类

    package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...

  3. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

  4. maven Dynamic Web Module 3.0 requires Java 1.6 or newer

      maven Dynamic Web Module 3.0 requires Java 1.6 or newer CreateTime--2018年4月19日16:56:42 Author:Mary ...

  5. linux 使用wc命令统计文件行数、字数及大小

    语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...

  6. 微信小程序:bindtap方法传参

    1.wxml <view bindtap="pay_again" data-name="{{orderList.jid}}" data-fee=" ...

  7. ArchLinux安装 LXDE

    http://wiki.lxde.org/zh/index.php?title=ArchLinux&variant=zh-cn 透过 pacman 安装 LXDE 大多数的最新 LXDE 套件 ...

  8. MySQL插入性能优化

    目录 MySQL插入性能优化 代码优化 values 多个 一个事务 插入字段尽量少,尽量用默认值 关闭 unique_checks bulk_insert_buffer_size 配置优化 inno ...

  9. OGG_GoldenGate日常维护(案例)

    2014-03-12 Created By BaoXinjian

  10. activity的onCreate参数 saveInstanceState

        写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是 ...