C# webbrowser 修改useragent
2021-06-20 16:06
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)] 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
文章链接:http://soscw.com/index.php/essay/96497.html