[C#] 纯文本查看 复制代码 public partial class Form1 : Form
{
bool isUserAgentSet = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser.Navigate(“[url]Http://someurl.com[/url]”);
}
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Check if the custom user agent is set
if (!isUserAgentSet)
{
//cancel the current request
e.Cancel = true;
this.isUserAgentSet = true;
//Navigate to the desired location using the custom user agent
this.webBrowser.Navigate(e.Url, e.TargetFrameName, null, "User-Agent: CustomUserAgent\r\n");
}
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//reset isUserAgentSet to prepare for the next navigate command
this.isUserAgentSet = false;
}
}
|