using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace WindowsFormsApplication2
{
public 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, );
}
/// <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();
}
}
}

Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)的更多相关文章

  1. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

  2. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...

  3. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...

  4. C#中实现WebBrowser控件的HTML源代码读写

    原文:C#中实现WebBrowser控件的HTML源代码读写 C#中实现WebBrowser控件的HTML源代码读写http://www.blogcn.com/user8/flier_lu/index ...

  5. C#在WinForm中重写ProgressBar控件(带%的显示)

    废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...

  6. vs2005中的WebBrowser控件的简单应用

    原文:vs2005中的WebBrowser控件的简单应用 这个控件被封装了一下,和以前的调用方式稍有不同.事件还是那几个,变化不大.方法变了不少.从网上能查到的资料不多,贴出一些代码来作参考.看看这段 ...

  7. winform中的dateTimePicker控件设置默认值为空

    winform中的dateTimePicker控件设置默认值为空   第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键

  8. 在VC中使用WebBrowser控件的两方法

    ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->Form a Type Library->C:/winnt/syste ...

  9. [转] WinForm实现移除控件某个事件的方法

    原文 WinForm实现移除控件某个事件的方法 本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary& ...

随机推荐

  1. Android 获取手机联系人信息

    //获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile

    一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...

  3. QString的不常见用法

    QString str("Hello"); QString str = "Hello"; static const QChar data[4] = { 0x00 ...

  4. 项目属性-->生成事件-->后期生成事件命令行

    以开源记牌器https://github.com/Epix37/Hearthstone-Deck-Tracker 为例,Hearthstone Deck Tracker项目中的后期生成事件命令行: i ...

  5. 国外成熟的程序交易系统的思路 z

    波涛(1998)在<系统交易方法>中提出,一个设计良好的交易系统,必须对投资决策的各个相关环节做出相应明确的规定,同时还必须符合使用者的心理特征.投资对象的统计特征以及投资资金的风险特征. ...

  6. Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/lock

    推荐博客:http://blog.sina.com.cn/s/blog_5c1450a8010188ju.html Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/ ...

  7. HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)

    Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现 ...

  8. pow(x,n) leecode

    https://oj.leetcode.com/problems/powx-n/ 提交地址 快速幂的使用,可以研究一下 public class Solution { public double po ...

  9. Mac下快捷键列表

    主界面 command + tab 切换程序 command + ` 在程序内切换界面 command + w 关闭界面 command + q 关闭程序 command + option + esc ...

  10. linux关于bashrc与profile的区别(转)

    转载自:http://www.cnblogs.com/hongzg1982/articles/2101792.html bashrc与profile的区别 要搞清bashrc与profile的区别,首 ...