WindowsRuntime一次设置User-Agent无须每次请求修改User-Agent
WindowsRuntime一次设置User-Agent无须每次请求修改User-Agent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public static class UserAgent { const int URLMON_OPTION_USERAGENT = 0x10000001; [DllImport("urlmon.dll", CharSet = CharSet.Ansi)] private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved); [DllImport("urlmon.dll", CharSet = CharSet.Ansi)] private static extern int UrlMkGetSessionOption(int dwOption, StringBuilder pBuffer, int dwBufferLength, ref int pdwBufferLength, int dwReserved); public static string GetUserAgent() { int capacity = 255; var buf = new StringBuilder(capacity); int length = 0; UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, buf, capacity, ref length, 0); return buf.ToString(); } public static void SetUserAgent(string agent) { var hr = UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, agent, agent.Length, 0); var ex = Marshal.GetExceptionForHR(hr); if (null != ex) { throw ex; } } public static void AppendUserAgent(string suffix) { SetUserAgent(GetUserAgent() + suffix); } } |
然后在应用启动位置使用
1 |
UserAgent.SetUserAgent("Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; WebView/3.0; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.15254"); |
如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流
原创文章,转载请注明本文链接地址(违者必究):WindowsRuntime一次设置User-Agent无须每次请求修改User-Agent