c# webbrowser的扩展使用
本文关键字: c#, webbrowser下面列几个实用的扩展应用:
1,修改ua
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;namespace XXX
{
class UserAgentHelper
{
private static string defaultUserAgent = null;
[DllImport(“urlmon.dll”, CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;
/// <summary>
/// 在默认的UserAgent后面加一部分
/// </summary>
public static void AppendUserAgent(string appendUserAgent)
{
if (string.IsNullOrEmpty(defaultUserAgent))
defaultUserAgent = GetDefaultUserAgent();
string ua = defaultUserAgent+”;”+appendUserAgent;
ChangeUserAgent(ua);
}
/// <summary>
/// 修改UserAgent
/// </summary>
public static void ChangeUserAgent(string userAgent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, userAgent.Length, 0);
}
/// <summary>
/// 一个很BT的获取IE默认UserAgent的方法
/// </summary>
private static string GetDefaultUserAgent()
{
WebBrowser wb = new WebBrowser();
wb.Navigate(“about:blank”);
while (wb.IsBusy) Application.DoEvents();
object window = wb.Document.Window.DomWindow;
Type wt = window.GetType();
object navigator = wt.InvokeMember(“navigator”, BindingFlags.GetProperty,
null, window, new object[] { });
Type nt = navigator.GetType();
object userAgent = nt.InvokeMember(“userAgent”, BindingFlags.GetProperty,
null, navigator, new object[] { });
return userAgent.ToString();
}
}
}
2,设置代理
#region 代理IP
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
//strProxy为代理IP:端口
private void RefreshIESettings(string strProxy)
{
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
const int INTERNET_OPEN_TYPE_DIRECT = 1;Struct_INTERNET_PROXY_INFO struct_IPI;
// Filling in structure
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi(“local”);// Allocating memory
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
if (string.IsNullOrEmpty(strProxy) || strProxy.Trim().Length == 0)
{
strProxy = string.Empty;
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;}
// Converting structure to IntPtr
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
}[DllImport(“wininet.dll”, SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
public class IEProxy
{
private const int INTERNET_OPTION_PROXY = 38;
private const int INTERNET_OPEN_TYPE_PROXY = 3;
private const int INTERNET_OPEN_TYPE_DIRECT = 1;private string ProxyStr;
[DllImport(“wininet.dll”, SetLastError = true)]private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
}private bool InternetSetOption(string strProxy)
{
int bufferLength;
IntPtr intptrStruct;
Struct_INTERNET_PROXY_INFO struct_IPI;if (string.IsNullOrEmpty(strProxy) || strProxy.Trim().Length == 0)
{
strProxy = string.Empty;
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
}
else
{
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
}
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi(“local”);
bufferLength = Marshal.SizeOf(struct_IPI);
intptrStruct = Marshal.AllocCoTaskMem(bufferLength);
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, bufferLength);}
public IEProxy(string strProxy)
{
this.ProxyStr = strProxy;
}
//设置代理
public bool RefreshIESettings()
{
return InternetSetOption(this.ProxyStr);
}
//取消代理
public bool DisableIEProxy()
{
return InternetSetOption(string.Empty);
}
}
#endregion
3:清理cookie
ShellExecute(IntPtr.Zero, “open”, “rundll32.exe”, ” InetCpl.cpl,ClearMyTracksByProcess 2″, “”, ShowCommands.SW_HIDE);
//使用
public enum ShowCommands : int
{SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
[DllImport(“shell32.dll”)]
static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
/*
Temporary Internet Files (Internet临时文件)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
Cookies
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
History (历史记录)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
Form. Data (表单数据)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
Passwords (密码)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
Delete All (全部删除)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
*/
4: 模拟键入
模拟按键在.Net中有三种方式实现。
第一种方式:System.Windows.Forms.SendKeys
组合键:Ctrl = ^ 、Shift = + 、Alt = %
模拟按键:A
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send(“{A}”);
}
模拟组合键:CTRL + A
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Focus();
SendKeys.Send(“^{A}”);
}
SendKeys.Send // 异步模拟按键(不阻塞UI)
SendKeys.SendWait // 同步模拟按键(会阻塞UI直到对方处理完消息后返回)
第二种方式:keybd_event
模拟按键:A
[DllImport(“user32.dll”, EntryPoint = “keybd_event”, SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
keybd_event(Keys.A, 0, 0, 0);
}
模拟组合键:CTRL + A
public const int KEYEVENTF_KEYUP = 2;private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Focus();
keybd_event(Keys.ControlKey, 0, 0, 0);
keybd_event(Keys.A, 0, 0, 0);
keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
}
上面两种方式都是全局范围呢,现在介绍如何对单个窗口进行模拟按键
模拟按键:A / 两次
[DllImport(“user32.dll”, EntryPoint = “PostMessageA”, SetLastError = true)]
public static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);public const int WM_CHAR = 256;
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
PostMessage(textBox1.Handle, 256, Keys.A, 2);
}
模拟组合键:CTRL + A如下方式可能会失效,所以最好采用上述两种方式
public const int WM_KEYDOWN = 256;
public const int WM_KEYUP = 257;private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Focus();
keybd_event(Keys.ControlKey, 0, 0, 0);
keybd_event(Keys.A, 0, 0, 0);
PostMessage(webBrowser1.Handle, WM_KEYDOWN, Keys.A, 0);
keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
5: 设置默认IE版本
RegistryKey rk = Registry.LocalMachine;
RegistryKey rks = rk.CreateSubKey(“SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION”);/*32
SOFTWARE
Microsoft
Internet Explorer
Main
FeatureControl
FEATURE_BROWSER_EMULATION
*/
rks.SetValue(“abc.exe”, “11001”, RegistryValueKind.DWord);
rk.Close();// 请勿使用调试版本 否则无效的,