C # WebBrowser Modify UserAgent

Source: Internet
Author: User

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 needed the user agent on the Web browser control within A Windows Forms project. I have since seen this question pops up quite a bit around the place, so I ' m going to show you II ways you can accomplish This.

You can check the 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 are an example of the 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 your want to navigate and easy enough.

The second paramter is the Target Frame. Here I had 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 could use this would be ' _blank ', which sends the navigation to a new window. If we were to use this from our application, it would open the default Web browser.

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

The last parameter was what we were interested in for the purpose of this tutorial. Here's 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 so you navigate To would recieve this as your User Agent.

2. Using api:urlmksetsessionoption
The second is the use of 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 The only-to-be able to change it's by restarting the process. So issue.

To access the API from Urlmon.dll, we need to use Interop. We can use the 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 are the integer of the option we wish to set. You can check out what options is available at:http://msdn.microsoft.com/en-us/library/ms775125 (vs.85). aspx for the Purp OSE of changing the user agent, we use Urlmon_option_useragent.

The second parameter is the buffer which contains the new settings. So-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 are 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 would be a troublesome if we want to keep changing the user agent, as we don ' t want to hard code the string and length. So are 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 can see, both of these methods has 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 ag ENT for the entire session. They can both be used to accomplish the same thing, so which one you are up to you. Enjoy =)

C # WebBrowser Modify UserAgent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.