问题
I'd like to retrieve a message from a contact on Whats App (with user's permission, of course, I thought something like oAuth) but as far I found, what apps doesn't have an API. So I tried load the web version in a WebBrowser and get the messag from there but I can't make it work.
It start loading the page to request the Qr code to be scanned but it redirects to a page saying the current browser isn't supported. So I tried to use emulation mode on IE, setting it to IE11 and changing http user agent to proper IE11's but it doesn't work either. How can I fix this?
Here's my current code:
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);
}
}
}
and I try open like this:
webBrowser1.Navigate(@"http://web.whatsapp.com/");
AA totally different solution to accomplish this is very welcome.
回答1:
If you browse https://web.whatsapp.com/ using Internet Explorer you will see supported browsers in the home page which says:
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)
So it seems currently the site doesn't support Internet Explorer .As an alternative you can use Cefsharp browser. To do so, install a suitable CefSharp.WinForms nuget package into visual studio and then add the control to your form and run the application:
var browser = new CefSharp.WinForms.ChromiumWebBrowser("https://web.whatsapp.com/");
browser.Dock = DockStyle.Fill;
this.Controls.Add(browser);
After you run the program, it shows a QR code which if you scan using your phone you will be logged in.
To scan the QR code, you should have WhatsApp installed on your mobile. Then open the application → CHATS → Open menu → Choose WhatApp Web then scan the code.
来源:https://stackoverflow.com/questions/40774833/how-can-i-get-a-message-from-a-specific-contact