利用ASP.NET WebClient的UploadData來做一個自動登入網頁的小程式
前陣子看到有人提到這方面的問題....
都沒什麼時間去研究..呵呵..可能工作太忙了吧...
最近抽空來研究這個問題...也實作了一個demo程式...
分享給大家呀....
首先要準備一個登入的頁面...Login.aspx
然後再透過一個AutoLogin.aspx的網頁自動登入Login.aspx那頁...
c#範例...
login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="uid:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="pwd:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="uid:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="pwd:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login" />
</div>
</form>
</body>
</html>
login.aspx.cs





























AutoLogin.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoLogin.aspx.cs" Inherits="AutoLogin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>AutoLogin</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="358px" TextMode="MultiLine" Width="542px"></asp:TextBox></div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>AutoLogin</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="358px" TextMode="MultiLine" Width="542px"></asp:TextBox></div>
</form>
</body>
</html>
AutoLogin.aspx.cs




















//__VIEWSTATE的值先到登入頁面檢視原始碼,並且Copy __VIEWSTATE的值




WebClient webClient = new WebClient();

webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

byte[] postData = Encoding.Default.GetBytes(postString);

byte[] responseData = webClient.UploadData(uriString, "POST", postData);

string srcString = Encoding.UTF8.GetString(responseData);

//取得登入頁按下Button1 postback後的結果



執行結果:
Login.aspx
AutoLogin.aspx
參考網址:
http://www.cnblogs.com/thunderdanky/articles/819156.html
http://www.blueshop.com.tw/board/show.asp?subcde=BRD200802261921229EU&fumcde=FUM20041006161839LRJ