C# webbrowser 修改useragent

來源:互聯網
上載者: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 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 =)

C# webbrowser 修改useragent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.