2

I would like to write a Qt application which uses Webkit as its gui to get data from a server and display it. I got it working unter Linux and OS X without problems but under windows the XMLHttpRequest always returns status 0 and I don't know why. Here is the pyqt code I use:

import sys, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.page().settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'index.html'))
url = "file://localhost/" + path
web.load(QUrl(url))
web.show()
sys.exit(app.exec_())

and here is html HTML/JS I use to test it:

<!DOCTYPE html>
<title>TEST</title>
<h1>TEST</h1>
<div id="test"></div>
<script type="text/javascript">
    function t(text) { document.getElementById("test").innerHTML = text }

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if(this.status != 0)
            t(this.responseText)
        else
            t("Status is 0")
    }
    xhr.open("GET", "https://jeena.net/")
    xhr.send()
</script>

On Linux it opens a new Window with a WebKit view in it, loads html local index.html file into it and renders it which shows the TEST headline. After that it runs the XMLHttpRequest code to get a websites content and set it with innerHTML into the prepared div.

On windows it loads and shows the title but then when it runs the xhr code the status is always just 0 and it never changes, no matter what I do.

As far as I understand LocalContentCanAccessRemoteUrls should make it possible for the xhr to get that content from the remote website even on windows, any idea why this is not working? I am using Qt version 4.9.6 on my windows machine and python v2.7.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jeena
  • 2,172
  • 3
  • 27
  • 46

1 Answers1

2

I think there are two simple attempts to solve this problem.

My first thinking is that it can be due to cross domain request. Seems that there is no easy way to disable cross domain protection in QWebkit. I got the information from this stackoverflow question:

QtWebkit Same-Origin-policy

As stated in the accepted answer:

"By default, Qt doesn't expose method to disable / whitelist the same origin policy. Extended the same (qwebsecurityorigin.cpp) and able to get it working."

But since you've got everything working on linux and mac, the above may not be the cause.

Another possibility is you don't have openssl enabled with your Qt on windows. Since I noticed you have requested to a https page, which should require openssl. You can change the page to a http one to quick test this possibility.

Community
  • 1
  • 1
Min Lin
  • 3,177
  • 2
  • 19
  • 32
  • I would also guess that openssl might be the issue. See also http://stackoverflow.com/questions/3444507/pyqt-qtwebkit-behind-a-proxy – jturcotte Apr 02 '13 at 12:53
  • Yes it is true, with http it works fine, sadly most of the domains are https which this app will use. The thing is that I installed SSL already and QtNetwork.QSslSocket.supportsSsl() returns True but I still have the same problem. – Jeena Apr 02 '13 at 21:07
  • Ok, when I ignore the ssl errors like proposed here http://stackoverflow.com/questions/8362506/qwebview-qt-webkit-wont-open-some-ssl-pages-redirects-not-allowed then everything works fine. It is a _really_ uggly workaround but at least the app shows the data now. I will accept the answer because it got me after weeks of trial and error on the right track, Thank you very very much! If you guys know a fix so I don't need to ignore the ssl errors then I would be glad to hear it. – Jeena Apr 02 '13 at 21:55
  • @Jeena could you show some information on the SSL error? Since its not reproducible on my windows machine. I'm using qt5. – Min Lin Apr 03 '13 at 01:38
  • It says: "The issuer certificate of a locally looked up certificate could not be found The root CA certificate is not trusted for this purpose." One example domain is: https://jeena.tent.is – Jeena Apr 03 '13 at 07:09