Introduction
This code guides you to change the User Agent in any browser.
This is a simple mobile web browser for PC. To view mobile web sites in PC (Windows):
https://sourceforge.net/projects/mobile-browser/
Background
Mobile version of Browser (Mobile-browser). Browsing like if you're on your Mobile Device supports various USER-AGENTS for browsing like (iPhone, Nokia Windows Phone, Android, Google Chrome, Firefox). This is simple mobile web browser for PC/windows
You can now open any mobile website in PC like Facebook, YouTube. Google, etc. Client/User agents are Android, iOS, iPhone, iPad, Chrome, Firefox, Internet Explorer 7, IE8 WP8, update will be coming soon adding some new user agent, and changing UI & improve performance and remove bugs. Required .NET Framework 3.5 or 4.0
Screen Shots
Using the Code
The code is so simple, when we change the user agent the file urlmon.dll works and it change the user interface of any website to their mobile format. Note: Your have to add the following reference to import the DLL file:
System.Runtime.InteropServices; and urlmon.dll file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MobileBrowser
{
public partial class Form1 : Form
{
[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;
public void ChangeUserAgent(string Agent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0);
}
string and_40 = "Mozilla/5.0 (Linux; Android 4.0.4; DROID RAZR Build/6.7" +
".2-180_DHD-16_M4-31) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/";
string iphone_30 = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X;" +
" en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
string ipad = "Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit" +
"/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";
string and_23 = "Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 " +
"Build/FRF91) App3leWebKit/53.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
string chrome_13 = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 " +
"(KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
string ie_8 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; " +
"Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)";
string ie_10_win8 = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone" +
" 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)";
Create function for when user hit Enter then navigate to the URL that in TextBox
private void TextBoxUrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
webBrowser1.Navigate(TextBoxUrl.Text);
MessageBox.Show("You press Enter");
}
}
When user click on GO button
private void toolStrip_GoButton_Click(object sender, EventArgs e)
{
try
{
if (radioButton1.Checked == true)
{
ChangeUserAgent(and_40);
webBrowser1.Navigate(TextBoxUrl.Text, "_self",
null, "User-Agent: {0}" + and_40);
labelurl.Text =Convert.ToString(webBrowser1.Url.AbsolutePath);
}
else if (radioButton2.Checked == true)
{
ChangeUserAgent(and_23);
webBrowser1.Navigate(TextBoxUrl.Text, "_self",
null, "User-Agent: {0}" + and_23);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri);
}
else if (radioButton3.Checked == true)
{
ChangeUserAgent(iphone_30);
webBrowser1.Navigate(TextBoxUrl.Text, "_self", null,
"User-Agent: {0}" + iphone_30);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri);
}
if (radioButton4.Checked == true)
{
ChangeUserAgent(ipad);
webBrowser1.Navigate(TextBoxUrl.Text, "_self",
null, "User-Agent: {0}" + ipad);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri);
}
else if (radioButton5.Checked == true)
{
ChangeUserAgent(chrome_13);
webBrowser1.Navigate(TextBoxUrl.Text, "_self", null,
"User-Agent: {0}" + chrome_13);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri);
}
else if (radioButton6.Checked == true)
{
ChangeUserAgent(ie_8);
webBrowser1.Navigate(TextBoxUrl.Text, "_self",
null, "User-Agent: {0}" + ie_8);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri);
}
else if (radioButton_WP8.Checked == true)
{
ChangeUserAgent(ie_10_win8);
webBrowser1.Navigate(TextBoxUrl.Text, "_self",
null, "User-Agent: {0}" + ie_10_win8);
labelurl.Text = Convert.ToString(webBrowser1.Url.AbsoluteUri)
}
}
catch(Exception exce)
{
MessageBox.Show("have an error occured","Error!");
}
}
Other Buttons
private void toolStrip_FwdButton_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void toolStrip_BackButton_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void toolStrip_RefreshButton_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
private void toolStrip_StopButton_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
Files Include
- urlmon.dll in bin/
- size: 1.0mb
This article is only for beginners to how to change User interface (user agent) in any web browser
History
This is my second article/tip. I know it has many mistakes. I hope to make my next article better than this.
next update will be coming soon