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.

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 =)

时间: 2024-11-25 23:15:19

C# webbrowser 修改useragent的相关文章

iOS UIWebView 修改user-agent

我的需求是不光要能更改user-agent,而且要保留WebView 原来的user-agent 信息,也就是说我需要在其上追加信息.在stackOverflow上搜集了多方答案,最终汇总的解决方案如下: 在启动时,比如在AppDelegate 中添加如下代码: //get the original user-agent of webview UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString *

火狐浏览器修改useragent伪装成微信浏览器

1.在浏览器地址栏输入“about:config” 2.新建字符串“general.useragent.override” 3.输入伪装的UserAgent值 iPhone版本 Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2 安卓版 Mozilla/5.0 (Linux; U; Android

Chrome浏览器修改user-agent,伪装其他浏览器,附带微信、支付宝user-agent

1,windows上F12,Mac上Alt+Command+I打开浏览器调试工具. 2,右下角的点点开,选择More tools,展开之后选择Network conditions. 3,下方会打开一个tab.里面是关于浏览器网络的一些设置.是否支持cache,选择什么类型的网络,user-agent等. 4,修改user-agent,可以伪装成其他浏览器. 5,如微信,支付宝的user-agent 微信:Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; G

firefox修改user-agent

让firefox对web服务器伪装成任意浏览器,找一个iphone的useragent,瞬间firefox变身iPhone有木有,一般人我不告诉他嘿嘿 1.firefox地址栏中输入about:config回车,然后点击“我保证会小心”按钮 2.在页面中的搜索框里输入“general.useragent.override”,如果下面有值,则可以双击修改.如果没有则需要新建一个 新建“general.useragent.override”方法如下: 页面中点右键,选择  新建->字符串 然后输入g

Selenium中通过修改User-Agent标识将PhantomJS伪装成Chrome浏览器

文章首发个人博客:http://zmister.com/archives/179.html Python爬虫.GUI开发.渗透测试.机器学习,尽在http://zmister.com/ 在写爬虫的过程中,出于系统环境或是效率的问题,我们经常使用PhantomJS作为Selenium操纵的浏览器webdriver,而不是直接使用Chrome或FireFox的webdriver,尽管后者更加直观. PhantomJS的优点虽然很多,但是缺点却也不少,有一个不能称之为缺点的缺点就是,PhantomJS

Delphi Webbrowser 修改 textarea 值 百度

有个按钮 调用  <a href="#" onclick="$.ajax({url: '/redmine/journals/edit/29606.js', type: 'get'}); return false;" title="编辑"><img alt="Edit" src="/redmine/images/edit.png?1409696303" /></a> red

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

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 { priva

修改浏览器的User-Agent来伪装你的浏览器和操作系统

近期很多文章都提到了User-Agent (UA) 字符串,但大部分网友都不知道这个东西有什么用处.其实简单的说User-Agent就是客户端浏览器等应用程序使用的一种特殊的网络协议,在每次浏览器(邮件客户端/搜索引擎蜘蛛)进行 HTTP 请求时发送到服务器,服务器就知道了用户是使用什么浏览器(邮件客户端/搜索引擎蜘蛛)来访问的.既然是人为规定的协议,那么就是说不管什么浏览器,默认的UA都是可以更改的.有时候为了达到一些不可告人的目的,我就经常需要更改一下UA的设置,比如伪装一下浏览器类型,比如

修改Firefox的User-Agent,伪装修改秘籍

火狐浏览器修改userAgent的办法一: 在火狐浏览器地址栏输入"about:config",按下回车进入设置菜单. 找到"general.useragent.override",如果没有这一项,则点右键"新建"->"字符串",输入这个字符串. 将其值设为自己想要的UserAgent(下面附有常见UA) 火狐浏览器修改userAgent的办法二: user Agent Switcher插件.下载地址:https://a