using System; using System.Windows.Forms; namespace WebBrowserExample { public partial class Form1 : Form { private string viewStateValue; public Form1() { InitializeComponent(); webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted; webBrowser1.Navigate("https://your-target-url.com"); } private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { // 確保網頁已經加載完成 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) { // 獲取 __VIEWSTATE var viewStateElement = webBrowser1.Document.GetElementById("__VIEWSTATE"); if (viewStateElement != null) { viewStateValue = viewStateElement.GetAttribute("value"); MessageBox.Show("獲取到的 VIEWSTATE: " + viewStateValue); // 設置新的 __VIEWSTATE 值 SetViewState(viewStateValue); } else { MessageBox.Show("__VIEWSTATE 元素未找到。"); } } } private void SetViewState(string newViewState) { // 使用 JavaScript 設置 __VIEWSTATE 的值 string script = $"document.getElementById('__VIEWSTATE').value = '{newViewState}';"; webBrowser1.Document.InvokeScript("eval", new object[] { script }); // 你可以在這裡進行其他操作,比如提交表單 // webBrowser1.Document.GetElementById("submitButton").InvokeMember("click"); } } }