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

引用\[1\]中提到了在VS2005中使用WebBrowser控件的简单应用,但是没有具体提到如何修改字体。引用\[2\]中提到了在WPF中嵌入WinForm中的WebBrowser控件,但同样没有提到如何修改字体。引用\[3\]中提到了使用JavaScript来修改字体的方法,但是这种方法不推荐使用。根据提供的引用内容,没有明确的方法来修改WebBrowser控件中的字体。 如果你想在C#修改WebBrowser控件中的字体,可以尝试使用JavaScript来实现。你可以通过调用WebBrowser控件的Document属性来获取网页的DOM对象,然后使用JavaScript来修改字体样式。例如,你可以使用以下代码来修改字体大小和行高: ```csharp webBrowser1.Document.InvokeScript("execScript", new object\[\] { "document.body.style.fontSize = '16px'; document.body.style.lineHeight = '1.5';", "JavaScript" }); ``` 这段代码会将字体大小设置为16像素,行高设置为1.5倍。你可以根据需要修改这些值来实现你想要的效果。 请注意,这种方法只适用于加载了网页内容的WebBrowser控件,对于本地HTML文件可能会有一些限制。另外,使用JavaScript来修改字体可能会影响网页的布局和样式,所以请谨慎使用。 #### 引用[.reference_title] - *1* *2* [C#中实现WebBrowser控件的HTML源代码读写](https://blog.csdn.net/weixin_29416629/article/details/117833349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [关于webbrowser更改字体大小的方法](https://blog.csdn.net/weixin_30740295/article/details/96554205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值