http://www.lukepaynesoftware.com/articles/programming-tutorials/changing-the-user-agent-in-a-web-browser-control/

Changing the User Agent in a web browser control

Changing the User Agent for C# Projects

A short time ago I completed a project for a company who needed to
change the user agent on the web browser control within a Windows Forms
project. I have since seen this question pop up quite a bit around the
place, so I’m going to show you two ways you can accomplish this.

You can check your User Agent at http://whatsmyuseragent.com/

1. When using webBrowser.Navigate, specify the additionalHeaders parameter.
When you call the Navigate method for a web browser control, the fourth
parameter let’s you specify any other headers that you would like to
send along with the request. You can view a list of HTTP headers at: http://en.wikipedia.org/wiki/List_of_HTTP_headers.

One header you can send is the “User-Agent”. Here is an example of this is use:

webBrowser1.Navigate ("http://www.whatsmyuseragent.com", "_self" , null, "User-Agent: Luke's Web Browser");

Let me explain the Navgiate method a little further in detail:

The first parameter specifys a string of a URL to which you want to navigate, easy enough.

The second paramter is the Target Frame. Here I have specified _self which says to navigate to the website inside the same webbrowser control. It is the same as not specifying a Target Frame. Another frame you could use here would be “_blank”, which sends the navigation to a new window. If we were to use that from our application, it would open the default web browser.

The third parameter contains any post data which you might want to send. So for example, if you wanted to fill out a form, you could specify the details here and the data would be send along with the request.

The last parameter is what we are interested in for the purpose of this tutorial. Here you could send any of the HTTP headers as previously shown in the Wikipedia article. The header to change the user agent is “User-Agent”. Simple specify: “User-Agent: mywebbrowser” and replace mywebbrowser with your own string and the website that you navigate to will recieve this as your User Agent.

2. Using API: UrlMkSetSessionOption
The second way is the use the API from urlmon.dll. I am told
that previous to Internet Explorer 8 using this API would set the User
Agent for the entrie process session, the only way you are able to
change it is by restarting the process. So you may run in to this issue.

To access the API from urlmon.dll, we need to use Interop. We can use this by adding to our project:

using System.Runtime.InteropServices;

To Import the API:

[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;

The first parameter here is the integer of the option we wish to set. You can check out what options are available at: http://msdn.microsoft.com/en-us/library/ms775125(VS.85).aspx for the purpose of changing the user agent, we use URLMON_OPTION_USERAGENT.

The second parameter is the buffer which contains the new settings. So for us this is the new user agent string.

The third parameter is the length of the buffer. So the length of “Luke’s Web Browser” is 18.

The final parameter is reserved, this must be set to 0.

So after adding the code to use the API, we can actually make use of it like this:

UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, "Luke's Web Browser", 18, 0);

This will be troublesome if we want to keep changing the user agent, as we don’t want to hard code the string and length. So here is a nice little method we could use instead:

public void ChangeUserAgent(string Agent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0);
}

So to call this we simply use:

ChangeUserAgent("Luke's Web Browser");

Alot easier isn’t it?

Conclusion

So, as you can see, both of these methods have their advantages and disadvantages.Using the Navigate method only sets the user agent on a per call request, where as using the API sets the user agent for the entire session. They can both be used to accomplish the same thing, so which one you use is up to you. Enjoy =)

C# webbrowser 修改useragent的更多相关文章

  1. 火狐浏览器修改userAgent

    火狐浏览器修改userAgent的办法: 在火狐浏览器地址栏输入“about:config”,按下回车进入设置菜单. 找到“general.useragent.override”,如果没有这一项,则点 ...

  2. 解决和排查 "必须使用适当的属性和方法修改 User-Agent" 错误时遇到的一些坑

    解决 必须使用适当的属性和方法修改 User-Agent 错误 问题描述:近在项目中有一个需求为需要在 Http 的Header里面添加一个User-Agent参数,当请求时.项目本身的目标框架是 . ...

  3. Selenium RemoteWebDriver 利用CDP修改User-Agent

    地球人都知道,如果使用selenium时要修改user-agent可以在启动浏览器时添加配置项,如chromeOptions.addArguments("user-agent=xxx&quo ...

  4. 通过Nginx(OpenResty)修改UserAgent

    通过OpenResty修改UserAgent,非常简单,Demo里做了多次反向代理是为了日志输出显示效果.实际应用中不必这么麻烦. 浏览器访问如下地址即可 http://127.0.0.1:10090 ...

  5. C# 修改webbrowser 的 useragent

    Also, there is a refresh option in the function (according to MSDN). It worked well for me (you shou ...

  6. 手动修改user-agent

    1. 在浏览器地址栏输入 about:config.弹出对话框:

  7. firefox修改user-agent

    让firefox对web服务器伪装成任意浏览器,找一个iphone的useragent,瞬间firefox变身iPhone有木有,一般人我不告诉他嘿嘿 1.firefox地址栏中输入about:con ...

  8. WebBrowser修改默认白色背景

      背景:在使用Winform的WebBrowser显示网页的时候,在网页还未加载完成之前会显示白色,刚好网页内容呈现黑色,这样视觉效果上就十分差,想把这层白色的去掉. 试了很久之后发现根本去不掉,应 ...

  9. Delphi Webbrowser 修改 textarea 值 百度

    有个按钮 调用  <a href="#" onclick="$.ajax({url: '/redmine/journals/edit/29606.js', type ...

随机推荐

  1. git学习笔记10-新开发的功能不想要了-强行删除分支

    添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支. 现在,你终于接 ...

  2. Linux中变量#,@,0,1,2,*,$$,$?的含义

    $# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该shell脚本的第一个参数 $ 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示所有向 ...

  3. SAP FI/CO凭证不一致的解决办法

    First, use program RKACOR20 to delete the incorrect CO documents. OKBA - Transfer FI Documents to CO ...

  4. Git学习(1)Git 简介

    Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件. Git ...

  5. Linux的中断 & 中断和异常的区别

    参考 http://www.yesky.com/20010813/192117.shtml 结构化程序设计思想认为:程序 = 数据结构 + 算法.数据结构体现了整个系统的构架,所以数据结构通常都是代码 ...

  6. 【Todo】用python进行机器学习数据模拟及逻辑回归实验

    参考了这个网页:http://blog.csdn.net/han_xiaoyang/article/details/49123419 数据用了 https://pan.baidu.com/s/1pKx ...

  7. 【转载】C++异常机制的学习

    参考了这篇文章:http://blog.chinaunix.net/uid-24517549-id-4079174.html 关于线程 进程和线程的概念相信各位看官早已耳熟能详.在这里,我只想带大家回 ...

  8. android下基本json串的生成与解析

    以前就用过json串,不过是在java环境下面,如今转移到android环境下,java里面生成解析json串的jar包与android中自带的冲突,所以也只能用安卓自带的.   先前查网上的资料,感 ...

  9. hiho_1138_island_travel

    题目 二维平面上有n个点,每个点的横纵坐标均为非负整数.两个点之间的距离记为 min(abs(x1 - x2), abs(y1 - y2)),求从点1到达点n的最短路径长度. 比较容易想到使用最短路径 ...

  10. commonJS — 事件处理(for Event)

    for Event github: https://github.com/laixiangran/commonJS/blob/master/src/forEvent.js 代码 (function(w ...