Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

XMLHttpRequest in QML with run time proxy settings

From Wiki
Jump to: navigation, search

I have modified the exiting networkaccessmanagerfactory example to add the proxy setting dynamically. The example is very easy and will help to resolve lots of XMLHttpRequest queries in QML.

Article Metadata
Code Example
Installation file: none
Tested with
SDK: Qt SDK 1.1.4
Devices(s): Emulator
Platform(s): Symbian, Meego
Device(s): Symbian and Meego
Dependencies: None
Platform Security
Signing Required: Self
Capabilities: NetworkServices
Keywords: XMLHttpRequest, Proxy, QNetworkProxyQuery, QNetworkProxyFactory
Created: kkrish (27 Feb 2012)
Last edited: hamishwillee (30 May 2013)


Code snippets to make a async call using XMLHttpRequest.

    function getFrontPage(){
console.debug("getFrontPage IN")
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
console.debug("got the page asynchornously " + xmlhttp.responseText.substring(1, 1000))
} else {
console.log("responseText", xmlhttp.responseText);
}"GET", "");
console.debug("getFrontPage OUT")

Some time the code may not work because you network might be behind the proxy. So now you may need to set the proxy to get the response. You can hard code the proxy settings but what if you change the network? So you can set the proxy dynamically inside the code.

QNetworkProxyQuery npq(QUrl(""));
QList<QNetworkProxy> proxyList = QNetworkProxyFactory::systemProxyForQuery(npq);
qDebug() << "proxyList[0].hostName() : " << proxyList[0].hostName() + " : proxyList[0].port() : " <<proxyList[0].port() << " : proxyList.count : " << proxyList.count();

I just print the 0th element from the list. You may also make a loop to print all the available proxy.

Now set these proxy details to QNetworkAccessManager. For details info see the attached example.


Other Links

This page was last modified on 30 May 2013, at 06:34.
148 page views in the last 30 days.