Reputation: 3493
My application consists from two apps. And one of them may ask another to perform some commands through a REST call (call an URL on localhost). For this purposes we use QNetworkAccessManager
(for put, get and post request).
Now there is a problem - device may go offline sometimes and when he does it - for some reason we can't use rest calls through access manager. It seems that it happen when network configuration that it uses is destroyed (like disabling Wifi adapter etc). When this configuration is restored (enabled Wifi), access manager starts work again.
Another detail - when we start app while we are offline - it works regardless of online state. It may be related to this.
This reproduces on both Win and Mac.
So the question is how can i reliably use QNetworkAccessManager for this purposes irregardless of devices online state? We use this manager only for localhost REST calls. What default network configuration or behavior should i set?
Example of usage below:
mNetManager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setRawHeader("User-Agent", "AppName/1.0");
request.setUrl(QUrl(url));
*reply = mNetManager->get(request);
Edit: online state is not required, since i need this access manager only for accessing local URLs on browser
Upvotes: 2
Views: 1648
Reputation: 25871
It appears you can force network accessibility to get local content:
mNetManager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setRawHeader("User-Agent", "AppName/1.0");
request.setUrl(QUrl(url));
mNetManager->setNetworkAccessible(QNetworkAccessManager::Accessible); // <--
auto reply = mNetManager->get(request);
Upvotes: 3
Reputation: 4180
The QNetworkAccessManager
have stuff for network availability. Why not using it?
QNetworkAccessManager * mNetManager = new QNetworkAccessManager(this);
connect(mNetManager, &QNetworkAccessManager::networkAccessibleChanged,
this, &YourClass::slotExecutedWhenNetworkAccessibilityChanges);
NetworkAccessibility netAcc = mNetManager->networkAccessible();
switch (netAcc) {
case QNetworkAccessManager::Accessible:
// You are online.
break;
case QNetworkAccessManager::NotAccessible:
// You are offline.
break;
case QNetworkAccessManager::UnknownAccessibility:
default:
// You know nothing, Jon Snow.
break;
}
Upvotes: 1