C# webbrowser 修改useragent
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.
![]() |
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)]
|
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) |
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的更多相关文章
- 火狐浏览器修改userAgent
火狐浏览器修改userAgent的办法: 在火狐浏览器地址栏输入“about:config”,按下回车进入设置菜单. 找到“general.useragent.override”,如果没有这一项,则点 ...
- 解决和排查 "必须使用适当的属性和方法修改 User-Agent" 错误时遇到的一些坑
解决 必须使用适当的属性和方法修改 User-Agent 错误 问题描述:近在项目中有一个需求为需要在 Http 的Header里面添加一个User-Agent参数,当请求时.项目本身的目标框架是 . ...
- Selenium RemoteWebDriver 利用CDP修改User-Agent
地球人都知道,如果使用selenium时要修改user-agent可以在启动浏览器时添加配置项,如chromeOptions.addArguments("user-agent=xxx&quo ...
- 通过Nginx(OpenResty)修改UserAgent
通过OpenResty修改UserAgent,非常简单,Demo里做了多次反向代理是为了日志输出显示效果.实际应用中不必这么麻烦. 浏览器访问如下地址即可 http://127.0.0.1:10090 ...
- C# 修改webbrowser 的 useragent
Also, there is a refresh option in the function (according to MSDN). It worked well for me (you shou ...
- 手动修改user-agent
1. 在浏览器地址栏输入 about:config.弹出对话框:
- firefox修改user-agent
让firefox对web服务器伪装成任意浏览器,找一个iphone的useragent,瞬间firefox变身iPhone有木有,一般人我不告诉他嘿嘿 1.firefox地址栏中输入about:con ...
- WebBrowser修改默认白色背景
背景:在使用Winform的WebBrowser显示网页的时候,在网页还未加载完成之前会显示白色,刚好网页内容呈现黑色,这样视觉效果上就十分差,想把这层白色的去掉. 试了很久之后发现根本去不掉,应 ...
- Delphi Webbrowser 修改 textarea 值 百度
有个按钮 调用 <a href="#" onclick="$.ajax({url: '/redmine/journals/edit/29606.js', type ...
随机推荐
- Timer 实现2秒4秒连环炸
package com.cn.gbx; import java.util.Date; import java.util.Timer; import java.util.TimerTask; //cla ...
- MyEclipse + Tomcat 热部署问题
myEclipse设置对应的tomcat时,只需要在jdk的Optional Java VM arguments中添加如下设置: -Xms256m -Xmx512m-Dcom.sun.manageme ...
- [转]ubuntu下安装程序的三种方法
出处:http://www.cnblogs.com/xwdreamer/p/3623454.html 引言 在ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装de ...
- json与jsonp应用及其他ajax数据交互方式
1.json是数据交换格式,使用实例如下: $.getJSON( '/manage/asset/asset_delByIds.action', { 'ids':id }, function(data) ...
- Docker-创建一个mysql容器,并保存为本地镜像
查找docker hub上的镜像 [root@wls12c ~]$ docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysq ...
- linux之echo命令
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个提示 ...
- Mac 实用工具与问题解决
1.在dock上方的一长溜,被我拖成个方块了 删掉里面的字符,然后按esc 即可! 2.FIT输入法(Fun Input Toy),是苹果操作系统OS X上的免费中文输入法,支持全拼/双拼/全双混拼, ...
- 工作了3年的JAVA程序员应该具备什么技能?(zhuan)
http://www.500d.me/article/5441.html **************************************** 来源:五百丁 作者:LZ2016-03-18 ...
- -bash: jps: command not found
linux安装了jdk之后,打jps命令发现找不到这个命令: -bash: jps: command not found 查看java版本java -version,正常. java version ...
- hiho_1062_最近公共祖先
题目大意 给出一棵家谱树,树中的节点都有一个名字,保证每个名字都是唯一的,然后进行若干次查询,找出两个名字的最近公共祖先. 分析 数据量较小,对于每次查询都进行如下操作: 先找出person1到达根节 ...
