1. web 2.0, 測試建立在 aspx 上。
需要調用Session的時候,使用 Cookie 容器即可。
首先,確定 scriptManager 的 SupportsPartialRendering 屬性 為 true
<asp:ScriptManager ID="ScriptManager1" runat="server" SupportsPartialRendering="true">
</asp:ScriptManager>
2. 然後,在代碼中要訪問由 script manager 控制的頁面,
a,創建 uri
b,設定 host, port, 並且 set method,以及設置 requestParameter
c,把 contentType 設定為
application/x-www-form-urlencoded
d,然後,用 stream write 發送請求
這樣就可以發送帶 parameter 的 post 。
重要的要注意的地方,
千萬記得,在 response 得到後,關掉 response.. res.close();!
try
{
UriBuilder addressbuilder = new UriBuilder(hostText.Text.Trim());
addressbuilder.Port = int.Parse(portText.Text.Trim());
this.displayInfo(addressbuilder.Uri.ToString());
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
// SET POST
req.Method = "POST";
req.KeepAlive = true;
String reqestParameter = "ScriptManager1=actionUpdatePanel|defaultStockTimer" +
"&__EVENTTARGET=defaultStockTimer" +
"&__EVENTARGUMENT=" +
"&__EVENTVALIDATION=/wEWBQL8lJvABQL/wp2hCwLejM6fDwKl8a/HDAL22+6ODiRtlX7qRZMcbQchySeGh6QUKyAz" +
"&__ASYNCPOST=true";
byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = rebyte.Length;
Stream reqs = req.GetRequestStream();
reqs.Write(rebyte, 0, rebyte.Length);
reqs.Close();
if (req == null)
{
this.displayInfo("Request is null.");
}
else
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
Stream s = res.GetResponseStream();
StreamReader sr = new StreamReader(s);
this.displayInfo(sr.ReadToEnd());
sr.Close();
}
}
catch (Exception ex)
{
this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
}
Post 的數據,通過使用 fwptt 這個軟件可以得到。下載地址:http://fwptt.sourceforge.net/index.html
恩,繼續。
如果你想要測試,那麼還需要關掉網站的保護系統。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="isrBank.aspx.cs" Inherits="AWS_LB.isrBank" EnableEventValidation="false" %>
將 EnableEventValidation 設定為 false 即可。
不過這樣的話網站肯定就會有問題。
比如微軟提到的 Injection..
不過這次的測試就是為了 injection
所以恩。。你懂的。
最後一步,就是操作 Session
Session 的話,就需要 CookieContainer 了。
首先創建 CookieContainer 然後在發送 Request 的時候加入即可。
private CookieContainer cc;
public WLG()
{
InitializeComponent();
cc = new CookieContainer();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
UriBuilder addressbuilder = new UriBuilder(hostText.Text.Trim());
addressbuilder.Port = int.Parse(portText.Text.Trim());
this.displayInfo(addressbuilder.Uri.ToString());
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
// SET POST
req.CookieContainer = cc; // <<COOKIE CONTAINER HERE
req.Method = "POST";
req.KeepAlive = true;
String reqestParameter = "ScriptManager1=contentUpdatePanel|loginFuncButton" +
"&usernameText=" + usernameText.Text.Trim() +
"&passwordText=" + passwordText.Text.Trim() +
//"&__EVENTVALIDATION=/wEWCAL11oX8DQLejM6fDwKl8a/HDAL22+6ODgKn+4ShDwLpg8H7AwK/uYXQBwL/wp2hC2dv3TV/KVZmPt6Oiw3A1S+q8PP+" +
"&__EVENTTARGET=" +
"&__EVENTARGUMENT=" +
"&__ASYNCPOST=true" +
"&loginFuncButton=Login";
byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = rebyte.Length;
Stream reqs = req.GetRequestStream();
reqs.Write(rebyte, 0, rebyte.Length);
reqs.Close();
if (req == null)
{
this.displayInfo("Request is null.");
}
else
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
Stream s = res.GetResponseStream();
StreamReader sr = new StreamReader(s);
String content = sr.ReadToEnd();
this.displayInfo(content);
webBrowser1.DocumentText = content;
foreach (Cookie ck in res.Cookies)
{
this.displayInfo(ck.Name + "=" + ck.Value);
}
}
}
catch (Exception ex)
{
this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
}
需要調用Session的時候,使用 Cookie 容器即可。
try
{
UriBuilder addressbuilder = new UriBuilder("http://localhost/isrStockDetail.aspx");
addressbuilder.Port = int.Parse(portText.Text.Trim());
this.displayInfo(addressbuilder.Uri.ToString());
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
// SET POST
req.CookieContainer = cc;
req.Method = "POST";
req.KeepAlive = true;
String reqestParameter = "action=commentUser" +
"&stockName=NASDAQ:AAPL" +
"&ScriptManager1=UpdatePanel1|Button1" +
"&TextBox1=nihaoma" +
"&Button1=Push" +
"&__ASYNCPOST=true" +
//"&__VIEWSTATE=/wEPDwUJNjI1MDgxOTc5D2QWAgIDD2QWBAIDDw8WAh4EVGV4dAUHRVRSOkRQV2RkAgUPZBYCZg9kFgQCAQ8PFgIfAAUWTm8gY29tbWVudCBmb3IgRVRSOkRQV2RkAgMPDxYCHgdWaXNpYmxlZ2RkZPaHUZEAUJOnc6ZUSSkikUVC6ozH" +
//"&__EVENTVALIDATION=/wEWBAKI3d6ODwKM54rGBgLWlM+bAgLs0bLrBuOLecbcd4U2Nl48r9WiUeNCxXW5" +
"&__EVENTTARGET=" +
"&__EVENTARGUMENT=";
byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = rebyte.Length;
Stream reqs = req.GetRequestStream();
reqs.Write(rebyte, 0, rebyte.Length);
reqs.Close();
if (req == null)
{
this.displayInfo("Request is null.");
}
else
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
Stream s = res.GetResponseStream();
StreamReader sr = new StreamReader(s);
String content = sr.ReadToEnd();
this.displayInfo(content);
webBrowser1.DocumentText = content;
}
}
catch (Exception ex)
{
this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
}