user13551985
user13551985

Reputation:

VB.NET, How to change the User-agent of the web browser control

There is a way to change The User-Agent of web browser control and it works! I changed the User-Agent by using the following code:

WebBrowser1.Navigate("https://example.com/file.php", Nothing, Nothing, "User-Agent: Your User-Agent" + vbCrLf)

It works, But the problem is that this code works one time!

For example if you entered to this site "https://example.com/login.php" and the site has referred you to another page! The second page will use the default User-Agent of Microsoft visual studio

Let me clarify this,The problem is that the code shown above cannot use the User-Agent more than once, After the site redirect you to another page the web browser will use the default User-Agent

Upvotes: 0

Views: 1957

Answers (1)

CherryDT
CherryDT

Reputation: 29011

You can call the UrlMkSetSessionOption API function as described here, and additionally use URLMON_OPTION_USERAGENT_REFRESH to avoid the renavigation problem described here:

Module UserAgentChanger
    <Runtime.InteropServices.DllImport("urlmon.dll", CharSet:=Runtime.InteropServices.CharSet.Ansi)>
    Private Function UrlMkSetSessionOption(
        ByVal dwOption As Integer,
        ByVal pBuffer As String,
        ByVal dwBufferLength As Integer,
        ByVal dwReserved As Integer) As Integer
    End Function

    Const URLMON_OPTION_USERAGENT As Integer = &H10000001
    Const URLMON_OPTION_USERAGENT_REFRESH As Integer = &H10000002

    Public Sub SetUserAgent(ByVal UserAgent As String)
        UrlMkSetSessionOption(URLMON_OPTION_USERAGENT_REFRESH, vbNullString, 0, 0)
        UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, UserAgent, UserAgent.Length, 0)
    End Sub
End Module

Upvotes: 1

Related Questions