「C#開発」User-agentを設定するサンプルコード

サンプルコード
private static string defaultUserAgent = null;

[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;

/// <summary>
/// </summary>
public static void AppendUserAgent(string appendUserAgent)
{
if (string.IsNullOrEmpty(defaultUserAgent))
defaultUserAgent = GetDefaultUserAgent();

string usat = defaultUserAgent + “;" + appendUserAgent;
ChangeUserAgent(usat);
}

/// <summary>
/// UserAgent修正
/// </summary>
public static void ChangeUserAgent(string userAgent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, userAgent.Length, 0);
}

関数を呼び出す
public FormCft()
{
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
InitializeComponent();
AppendUserAgent(“CftLj");
this.labelcft.Text = GetDefaultUserAgent();
}

C#

Posted by arkgame