62,074
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>源页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
public static string PostAndGetHTML(string targetURL, CookieContainer cc, Hashtable param)
{
//prepare the submit data
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}
if (formData.Length > 0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
protected void Button1_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data.
string urlLogin = "http://localhost:2723/Calendar/WebCollection/FountainPage.aspx";
//do find the elementId that needed. check the source of login page can get this information
param.Add("TextBox1", "zyg");
param.Add("TextBox2", "123");
param.Add("TextBox3", "");
param.Add("Button1", "Button");
param.Add("__VIEWSTATE","/wEPDwUKMTI1MTk2NDQzM2Rkk6u ZldYY1+fzRUM5R vNvdCBjuHY=");
param.Add("__EVENTVALIDATION","/wEWBQL2mMyfAwLs0bLrBgLs0fbZDALs0Yq 1BQKM54rGBuvZsBxWq JfO2NPZRSrp68d7MNRQ");
string result = PostAndGetHTML(urlLogin, cc, param);
Response.Write(result);
}
protected void Button1_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data.
string urlLogin = "http://localhost:2723/Calendar/WebCollection/FountainPage.aspx";
//do find the elementId that needed. check the source of login page can get this information
param.Add("TextBox1", "zyg");
param.Add("TextBox2", "123");
param.Add("Button1", "Button");
param.Add("__VIEWSTATE", "/wEPDwULLTExNTc2NTI3OTlkZHbOdWF4uA82vzs960/N2fQQfeZD");
param.Add("__EVENTVALIDATION", "/wEWBAKs1+r2CQLs0bLrBgLs0fbZDAKM54rGBqc/HlccvtrW08kNqxhBoSJUnBKw");
string result = PostAndGetHTML(urlLogin, cc, param);
Response.Write(result);
}
public static string PostAndGetHTML(string targetURL, CookieContainer cc, Hashtable param)
{
//prepare the submit data
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}
if (formData.Length > 0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return result;
}