Created
June 7, 2011 08:53
-
-
Save magicshui/1011918 to your computer and use it in GitHub Desktop.
cx source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" | |
CodeBehind="Default.aspx.cs" Inherits="Chaxun._Default" %> | |
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> | |
</asp:Content> | |
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> | |
<asp:ScriptManager ID="ScriptManager1" runat="server"> | |
</asp:ScriptManager> | |
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> | |
<ContentTemplate> | |
<p> | |
<asp:Label ID="Label1" runat="server" Text="学号"></asp:Label> | |
<asp:TextBox ID="id" runat="server"></asp:TextBox> | |
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ForeColor="Red" ValidationExpression="20(0|1)\d{7}" | |
runat="server" ErrorMessage="学号格式不正确" ControlToValidate="id"></asp:RegularExpressionValidator> | |
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" | |
runat="server" ErrorMessage="学号不能为空" ForeColor="Pink" ControlToValidate="id"></asp:RequiredFieldValidator> | |
</p> | |
<p> | |
<asp:Label ID="Label2" runat="server" Text="密码"></asp:Label> | |
<asp:TextBox ID="pwd" runat="server" ToolTip="用户密码不会被保存" TextMode="Password" ></asp:TextBox> | |
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ForeColor="Red" runat="server" ErrorMessage="密码不能为空" | |
ControlToValidate="pwd"></asp:RequiredFieldValidator> | |
</p> | |
<p> | |
<asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" /> | |
</p> | |
<asp:Label ID="result" runat="server" Text="result:"></asp:Label> | |
</ContentTemplate> | |
</asp:UpdatePanel> | |
</asp:Content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
版权所有:magicshui | |
时间: 2011-6-6 | |
作者: magicshui | |
请遵守大陆cc创作共享协议,署名-非商业-相同方式分享 | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Net; | |
using System.Text; | |
using System.IO; | |
namespace Chaxun | |
{ | |
public partial class _Default : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
} | |
// 点击查询按钮时的事件 | |
protected void Button1_Click(object sender, EventArgs e) | |
{ | |
string back=Login(id.Text, pwd.Text); | |
string rank=getPage("http://202.206.1.163/bxqcjcxAction.do", null); | |
result.Text = rank==""?"用户名或密码错误":rank; | |
id.Text = ""; | |
pwd.Text = ""; | |
} | |
// 用户登陆认证 | |
private static string Login(string uid,string password) | |
{ | |
string paramList = "zjh="+uid+"&mm="+password; | |
string url="http://202.206.1.163/loginAction.do"; | |
HttpWebResponse res = null; | |
string result = ""; | |
try | |
{ | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); | |
req.Method = "POST"; | |
req.ContentType = "application/x-www-form-urlencoded"; | |
req.AllowAutoRedirect = false; | |
CookieContainer cookieContainer = new CookieContainer(); | |
req.CookieContainer = cookieContainer; | |
StringBuilder urlEn = new StringBuilder(); | |
char[] reserved = { '?', '=', '&' }; | |
byte[] bs = null; | |
if (paramList != null) | |
{ | |
int i = 0, j; | |
while (i < paramList.Length) | |
{ | |
j = paramList.IndexOfAny(reserved, i); | |
if (j == -1) | |
{ | |
urlEn.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i))); | |
break; | |
} | |
urlEn.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i))); | |
urlEn.Append(paramList.Substring(j, 1)); | |
i = j + 1; | |
} | |
bs = Encoding.UTF8.GetBytes(urlEn.ToString()); | |
req.ContentLength = bs.Length; | |
Stream newStream = req.GetRequestStream(); | |
newStream.Write(bs, 0, bs.Length); | |
newStream.Close(); | |
} | |
else | |
{ | |
req.ContentLength = 0; | |
} | |
res = (HttpWebResponse)req.GetResponse(); | |
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url)); | |
Stream recStream = res.GetResponseStream(); | |
Encoding encode = System.Text.Encoding.GetEncoding("GBK"); | |
StreamReader sr = new StreamReader(recStream, encode); | |
Char[] read = new Char[256]; | |
int count = sr.Read(read, 0, 256); | |
while (count > 0) | |
{ | |
String str = new String(read, 0, count); | |
result += str; | |
count = sr.Read(read, 0, 256); | |
} | |
} | |
catch (Exception) | |
{ | |
result = ""; | |
} | |
finally | |
{ | |
if (res != null) | |
{ | |
res.Close(); | |
} | |
} | |
return result; | |
} | |
// 获取页面数据的封装函数 | |
public static string getPage(String url, String paramList) | |
{ | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); | |
req.Referer = "http://102.106.1.163/menu/s_menu.jsp"; | |
CookieContainer cookieCon = new CookieContainer(); | |
req.CookieContainer = cookieCon; | |
req.CookieContainer.SetCookies(new Uri(url), cookieheader); | |
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); | |
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default); | |
string strResult = sr.ReadToEnd(); | |
sr.Close(); | |
return strResult; | |
} | |
public static string cookieheader { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment