关于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

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试