QWebView: Browser Not Supported
-
I want to view a webpage in a plugin I'm developing, but I'm running into an issue with QWebView. When I go to the URL "http://192.168.1.76" I get a error that says "Browser not supported...web interface only supports Google Chrome browser" I contacted support of the product I'm trying to pull an image from, and they said that I probably need to send the correct Request headers.
I tried using QWebEngineView because I read somewhere that it was Chromium based to see if that would resolve the issue, and it doesn't.
This is the function I use to get to the webpage
void PickItPlugin::on_pushButton_17_clicked() { ui->webView->setUrl(QUrl("http://192.168.1.76")); }
I use a webview from my UI made in Qt Creator.
This is the header I get when I inspect the image's URL in the Chrome Browser.
GET /2d_stream.jpg HTTP/1.1 Host: 192.168.1.76 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9
My Question is how do I set the Request header properly so I can access the image using QWebView?
-
You probably just need to override
userAgentForUrl
method in your subclass ofQWebPage
, and set object of your class into QWebViewNote that if site explicitly requires Chromium it might rely on some new features of web platform. Make sure that you are using QtWebKit 5.212
-
@Konstantin-Tokarev Thank you for your reply.
I hate to admit this, but I'm not sure how to subclass QWebpage and override the userAgentForUrl. I'm also not sure what I need to set the userAgentForUrl string to.
Here's what I have tried:
"webpage.h"
#include <QWebView> class WebPage: public QWebPage { public: WebPage(); QString userAgentForUrl(const QUrl & url); };
"webpage.cpp"
#include "webpage.h" WebPage::WebPage() { } QString WebPage::userAgentForUrl(const QUrl &url) { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"; }
"plugin.cpp"
void PickItPlugin::on_pushButton_17_clicked() { ui->webView->setUrl(QUrl("http://192.168.1.72")); WebPage page; page.userAgentForUrl(QUrl("http://192.168.1.72")); ui->webView->setPage(page); }
I know that there is a lot wrong with this, but I'm not sure exactly how I need to go about setting all of this up. Any help would be appreciated.
-
Your code looks correct. You can check if user-agent is really working using test page like https://whatsmyua.info/
-
Does this do that same thing as below?
QNetworkRequest request; request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/538.1 (KHTML, like Gecko) AUBOPE Safari/538.1"); ui->webView->load(request);
I'm still getting the Browser not supported error.
I've even tried this:
QNetworkRequest request; request.setRawHeader("Connection", "keep-alive"); request.setRawHeader("Cache-Control", "max-age=0"); request.setRawHeader("Upgrade-Insecure-Requests", "1"); request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/538.1 (KHTML, like Gecko) AUBOPE Safari/538.1"); request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"); request.setRawHeader("Accept-Encoding", "gzip, deflate"); request.setRawHeader("Accept-Langauge", "en-US,en;q=0.9"); request.setUrl(QUrl("http://192.168.1.72")); ui->webView->load(request);
-
Maybe it doesn't check request headers, but does JS testing of supported features