在C# winform程序里,webBrowser是一个很好用的,类似浏览器的控件。在模拟post,get请求时,有些时候HttpWebRequest和HttpWebResponse,会不能用,因为有些网站有“防”(加js等)这种请求,必须要浏览器来请求才可以,这样我们就需要把HttpWebRequest的cookie传到WebBrowser里面。
传cookie的话要用到InternetSetCookie这个API函数
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
引用命名空间:using System.Runtime.InteropServices;
调用示例:InternetSetCookie(“url", "cookiename", "cookievalue");
这样只要设置了cookie,在webBrowser里打开的窗口,会自动加上cookie的。
虽然webBrowser很好用,但有些时候,会弹出一些js窗口,还有脚本错误之类的东东。
1、对于脚本错误的话,webBrowser有个ScriptErrorsSuppressed属性,默认是false,不阻止,要想阻止这类错误,只要设成true,就可以了。
2、对于js窗口,我用了windows api 函数FindWindow等。
我封装了一个类,在程序中调用就可以实现关闭所有弹出的js窗口。
调用代码:CloseJsWindow.StartCloseWindow();
类代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace SUP
{
public class CloseJsWindow
{
[DllImport( " user32.dll " , EntryPoint = " FindWindow " , SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
[DllImport( " user32.dll " , EntryPoint = " FindWindowEx " , SetLastError = true )]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport( " user32.dll " , EntryPoint = " SendMessage " , SetLastError = true , CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, int lParam);
[DllImport( " user32.dll " , EntryPoint = " SetForegroundWindow " , SetLastError = true )]
private static extern void SetForegroundWindow(IntPtr hwnd);
private static string windowTitle = " Message from webpage " ;
private const uint BM_CLICK = 0xF5 ; // 鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
static System.Threading.Timer findTimer;
static AutoResetEvent autoEvent;
/// <summary>
/// 开始监视js弹出的窗口
/// </summary>
public static void StartCloseWindow()
{
autoEvent = new AutoResetEvent( false ); // 初始状态设置为非终止
TimerCallback timerDelegate = new TimerCallback(CloseWindow);
findTimer = new System.Threading.Timer(timerDelegate, autoEvent, 1000 , 800 ); // 每0.8秒钟查找一次
}
/// <summary>
/// 停止监视js弹出的窗口
/// </summary>
public static void StopCloseWindow()
{
autoEvent.WaitOne( 10 , false ); // 设置10毫秒,是让程序有一个等待,如果设为0,会一直弹窗口
findTimer.Dispose();
}
private static void CloseWindow( object state)
{
IntPtr hwnd = FindWindow( null , windowTitle); // 查找窗口的句柄
if (hwnd != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd, 0 , " Button " , " 确定 " ); // 获取确定按钮的句柄
SendMessage(hwndSure, BM_CLICK, (IntPtr) 0 , 0 ); // 发送单击消息
}
}
}
}
要想停止的话,只要调用 CloseJsWindow.StopCloseWindow()就行了。
下面这个是自动关闭由MessageBox.Show()弹出的对话框
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
namespace xmypkj
{
public class MessageBoxAuto
{
[DllImport( " user32.dll " , EntryPoint = " FindWindow " , SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
[DllImport( " user32.dll " , EntryPoint = " SendMessage " , SetLastError = true , CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, int lParam);
[DllImport( " user32.dll " , CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10 ;
private const uint BM_CLICK = 0xF5 ; // 鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
private static string title = " 提示信息 " ; // 查找窗口时,标题最好不要为空
static System.Threading.Timer findTimer;
static AutoResetEvent autoEvent;
public static void Show( string text)
{
StartCloseMessageBox( 3000 ); // 默认3秒关闭
MessageBox.Show(text, title);
StopCloseMessageBox();
}
public static void Show( string text, string caption)
{
StartCloseMessageBox( 3000 );
title = caption;
MessageBox.Show(text, caption);
StopCloseMessageBox();
}
public static void Show( string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, int millis)
{
StartCloseMessageBox(millis);
title = caption;
MessageBox.Show(text, caption, buttons, icon);
StopCloseMessageBox();
}
/// <summary>
/// 开始监视MessageBox
/// </summary>
private static void StartCloseMessageBox( int millis)
{
autoEvent = new AutoResetEvent( false ); // 初始状态设置为非终止
TimerCallback timerDelegate = new TimerCallback(CloseWindow);
findTimer = new System.Threading.Timer(timerDelegate, autoEvent, 2000 , millis);
}
/// <summary>
/// 停止监视MessageBox
/// </summary>
private static void StopCloseMessageBox()
{
autoEvent.WaitOne( 10 , false ); // 设置10毫秒,是让程序有一个等待,如果设为0,会一直弹窗口
findTimer.Dispose();
}
/// <summary>
/// 发送按钮单击消息关闭窗口
/// </summary>
/// <param name="state"></param>
private static void CloseWindow( object state)
{
IntPtr hwnd = FindWindow( null , title); // 查找窗口的句柄
if (hwnd != IntPtr.Zero)
{
PostMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
调用代码
MessageBoxAuto.Show("验证码识别组件加载失败!");