关于post一个aspx页面

比asp页面麻烦的多

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Web;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string htmlSource = "";
string postText = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTU2Nzk0MTg5N2QYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFFGhrTG9naW4wMSRSZW1lbWJlck1lBRpoa0xvZ2luMDEkTG9naW5JbWFnZUJ1dHRvbleoxuK8yk17MvbJTY9N6RN8eArq&hkLogin01%24UserName=test&hkLogin01%24Password=testtest&hkLogin01%24LoginButton=%E7%99%BB%E5%BD%95&__EVENTVALIDATION=%2FwEWBQLsiPXZAwKg6OuIAgL%2F%2FvnLAQL7xqzfAwK41sSoDiuwjoKdNoAZMHTsG2KXpS4KtmFQ";

CookieCollection hbCookieCollection_Cookie = hfCookieCollection_GetCookie("http://www.zlway.net/bbs/gg/login.aspx", postText, out htmlSource);

string finallyHtmlSource = hfGetHtmlByCookies("http://www.zlway.net/bbs/fgg/ReTie10.aspx?BKID=765725bf-a270-4859-a669-a7df0a72f8fe&TieID=d5cfbfaf-5d51-4b76-b9d5-1d0d26a150f1", hbCookieCollection_Cookie);
}

public CookieCollection hfCookieCollection_GetCookie(string url, string postText, out string strHtml)
{
Encoding encode = new UTF8Encoding();
byte[] data = encode.GetBytes(postText);

CookieCollection hbCookieCollection = new CookieCollection();
CookieContainer hbCookieContainer = new CookieContainer();
HttpWebRequest hbHttpWebRequest;
HttpWebResponse hbHttpWebResponse;
//请求cookies的格式
//构建即将发送的包头
hbHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
Cookie o = new Cookie("ASP.NET_SessionId", "o5ltdkb3e5kbtw55nuqfgkqy");
o.Domain = "www.zlway.net";

hbCookieContainer.Add(o);
hbHttpWebRequest.CookieContainer = hbCookieContainer;
hbHttpWebRequest.Accept = "*/*";
hbHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
hbHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
hbHttpWebRequest.Method = "POST";

hbHttpWebRequest.ContentLength = data.Length;
hbHttpWebRequest.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;

//写入
Stream hbStreamWriter = hbHttpWebRequest.GetRequestStream();
hbStreamWriter.Write(data, 0, data.Length);
hbStreamWriter.Flush();
hbStreamWriter.Close();

//发送请求获取响应内容
hbHttpWebResponse = (HttpWebResponse)hbHttpWebRequest.GetResponse();

System.IO.StreamReader sr = new System.IO.StreamReader(hbHttpWebResponse.GetResponseStream(), Encoding.UTF8); //gb2312//utf-8
strHtml = sr.ReadToEnd();
sr.Close();

//获取缓存内容
//要从Request中取cookie, 因为Server端重新设定了requst.cookies
hbCookieCollection = hbCookieContainer.GetCookies(hbHttpWebRequest.RequestUri);
return hbCookieCollection;
}

public string hfGetHtmlByCookies(string strUri, CookieCollection ccl)
{
CookieContainer hbCookieContainer = new CookieContainer();
HttpWebRequest hbHttpWebRequest;
HttpWebResponse hbHttpWebResponse;

//构建即将发送的包头
hbHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(strUri));
hbCookieContainer.Add(ccl);
hbHttpWebRequest.CookieContainer = hbCookieContainer;
hbHttpWebRequest.Accept = "*/*";
hbHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
hbHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
hbHttpWebRequest.Method = "GET";
hbHttpWebRequest.ContentLength = 0;

hbHttpWebRequest.AllowAutoRedirect = true;//这个和以下是我添加的
hbHttpWebRequest.MaximumAutomaticRedirections = 10;//自动重定向最大次数
hbHttpWebRequest.KeepAlive = true;
hbHttpWebRequest.Referer = "http://www.zlway.net/bbs/gg/login.aspx";
hbHttpWebRequest.ServicePoint.Expect100Continue = false;
hbHttpWebRequest.ProtocolVersion = new Version("1.1");

//发送请求获取响应内容
try
{
hbHttpWebResponse = (HttpWebResponse)hbHttpWebRequest.GetResponse();
}
catch
{
return "";
}

Stream stream;
stream = hbHttpWebResponse.GetResponseStream();
StreamReader sReader = new StreamReader(stream, Encoding.UTF8);
string strHtml = sReader.ReadToEnd();
sReader.Close();
stream.Close();

//返回值
return strHtml;
}
}

}

原贴:http://topic.csdn.net/u/20090222/06/8a001b21-f061-4d48-9de9-c8a212c6434d.html?2014331620

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的ASP.NET注册页面的代码示例: ```markup <!DOCTYPE html> <html> <head> <title>注册</title> </head> <body> <h2>注册</h2> <form method="post" action="register.aspx"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br> <label for="confirm_password">确认密码:</label> <input type="password" id="confirm_password" name="confirm_password" required><br> <label for="email">邮箱:</label> <input type="email" id="email" name="email" required><br> <input type="submit" value="注册"> </form> </body> </html> ``` 这个页面包括了一个表单,用户可以输入他们的用户名、密码、确认密码和电子邮件地址。当用户点击“注册”按钮时,表单将提交到名为“register.aspx”的页面进行处理。 在“register.aspx”页面中,您可以使用以下代码来处理表单提交: ```csharp protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { string username = Request.Form["username"]; string password = Request.Form["password"]; string confirm_password = Request.Form["confirm_password"]; string email = Request.Form["email"]; if (password != confirm_password) { // 密码和确认密码不匹配,显示错误消息 Response.Write("密码和确认密码不匹配!"); } else { // TODO: 将用户信息保存到数据库中 Response.Write("注册成功!"); } } } ``` 上面的代码使用了ASP.NET的“IsPostBack”属性来检查页面是否是在表单提交后加载的。如果是,它将从表单数据中获取用户名、密码、确认密码和电子邮件地址。然后,它将检查密码和确认密码是否匹配。如果不匹配,它将显示一个错误消息。如果密码和确认密码匹配,它将将用户信息保存到数据库中(这里我们没有实现)并显示注册成功的消息。 希望这可以帮助您开始编写注册页面
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值