如何从特定联系人那里获得消息?

How can I get a message from a specific contact?

我想从 Whats App 上的联系人检索消息(当然,在用户许可的情况下,我想到了类似 oAuth 的东西)但据我所知,哪些应用程序没有 API.所以我尝试在 WebBrowser 中加载网络版本并从那里获取消息,但我无法使其工作。

它开始加载页面以请求扫描二维码,但它重定向到一个页面,说当前浏览器不受支持。所以我尝试在 IE 上使用仿真模式,将其设置为 IE11 并将 http 用户代理更改为正确的 IE11,但它也不起作用。我该如何解决这个问题?

这是我当前的代码:

 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;
        const int URLMON_OPTION_USERAGENT_REFRESH = 0x10000002;
        const string usersAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetEmulation();
            ChangeUserAgent(usersAgent);
        }

        public static void ChangeUserAgent(string UserAgent)
        {
            UrlMkSetSessionOption(URLMON_OPTION_USERAGENT_REFRESH, null, 0, 0);
            UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, UserAgent, UserAgent.Length, 0);
        }

        public void SetEmulation()
        {
            const int BROWSER_EMULATION_IE11 = 0x2AF9;  
            var appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                             appName, BROWSER_EMULATION_IE11, RegistryValueKind.DWord);
        }

        void UnsetEmulation()
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true))
            {
                var appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
                key.DeleteValue(appName);
            }
        }
}

然后我尝试这样打开:

webBrowser1.Navigate(@"http://web.whatsapp.com/");

非常欢迎实现此目的的完全不同的解决方案。

如果您使用 Internet Explorer 浏览 https://web.whatsapp.com/,您将在主页中看到受支持的浏览器,其中显示:

We recommend using WhatsApp with one of the following browsers:

  • Google Chrome
  • Mozilla Firefox
  • Opera WhatsApp

also supports:

  • Microsoft Edge
  • Safari (MacOS 10.8+ Only)

所以目前该站点似乎不支持 Internet Explorer。作为替代方案,您可以使用 Cefsharp browser. To do so, install a suitable CefSharp.WinForms nuget 包到 visual studio,然后将控件添加到您的表单和 运行 申请:

var browser = new CefSharp.WinForms.ChromiumWebBrowser("https://web.whatsapp.com/");
browser.Dock = DockStyle.Fill;
this.Controls.Add(browser);

在您 运行 程序之后,它会显示一个二维码,如果您使用 phone 扫描该二维码,您将登录。

要扫描二维码,您应该在手机上安装 WhatsApp。然后打开应用程序→CHATS→打开菜单→选择WhatApp Web然后扫描代码。