Debug QWebView::load() error

Today, when I opened one of my qt program(normally this program will open a website on its GUI), I found it could not load and display the website as usual. I remember the network configuration has been changed from a dial connection to a direct network connection, which would cause the problem. In fact the program works(i.e. can load the website) well after dialing to a VPN.   And this is not the problem of the website itself because the program cannot connect to any website whether you use its domain name or ip address except the “http://localhost”. It must be due to the network settings on my computer. I try to adjust some configurations about network such as disabling/enabling dhcp, using/not using lmhosts searching, disabling netbios over TCP/IP, creating/deleting a hosts file, etc. Unfortunately none of these methods works. Since the program can load websites through VPN connections(it uses the QWebView::load() function to load urls.), it should not the problem of the qt libraries. I decide to debug the problem by adding some codes to the program.

You can not get the error information by the return value of QWebView::load() because the function always return void. By adding the following codes(refer to http://stackoverflow.com/questions/12555809/how-to-debug-qwebviews-failure-to-load-a-web-page), I can get a little information about what happens when QWebView::load() is executed:

QNetworkAccessManager*nam=page()->networkAccessManager(); connect(nam,SIGNAL(finished(QNetworkReply*)),this, SLOT(report(QNetworkReply*)));

void report(QNetworkReply*reply)
{
  int error=reply->error();
  QMessageBox::information(this,"error",QString("%1").arg(error));
}

 

Unfortunately, the error code got is always 1(QNetworkReply::ConnectionRefusedError), which means “the remote server refused the connection (the server is not accepting requests)”. This is apparently a misleading message considering what we talked about the problem.

Using wireshark, I find several ssdp(M-search…, ssdp:discover) packets are transferred on the network. Sometimes, some wpad packets are transmitted to get the wpad.dat file. This is a little weird.

Since the program works without a problem on dial network connections, I guess it may select a wrong network interface. When opening device manager and checking “show hide device” option, I can see several network devices. Of course my computer has only one physical network card(the local LAN network interface), the other devices are virtual net cards. To verify my suspicion, I use the following codes(http://stackoverflow.com/questions/31011888/how-to-check-a-type-of-a-network-interface-in-qt):

void CheckNetwork::showEvent(QShowEvent *event){
  textEdit->append("-----------------");
  textEdit->append("NetworkInterfaces");
  textEdit->append("-----------------");
  textEdit->append("UP AND RUNNING:");
  network_interface = new QNetworkInterface();
  foreach (QNetworkInterface interface, network_interface->allInterfaces()) {
    if((interface.flags() & QNetworkInterface::IsUp) && (interface.flags() & QNetworkInterface::IsRunning))
      textEdit->append("\t" + interface.name() + " " + interface.humanReadableName() + " " + interface.hardwareAddress());
  }
  textEdit->append("-----------------------------");
  textEdit->append("Network Configuration Manager");
  textEdit->append("-----------------------------");
  textEdit->append("ACTIVE:");
  network_configuration_manager = new QNetworkConfigurationManager(this);
  QObject::connect(network_configuration_manager, SIGNAL(updateCompleted()), this, SLOT(network_configuration_manager_updateCompleted()));
  network_configuration_manager->updateConfigurations();
}

void CheckNetwork::network_configuration_manager_updateCompleted(){
  foreach (QNetworkConfiguration configuration, network_configuration_manager->allConfigurations(QNetworkConfiguration::Active)) {
    textEdit->append("\t" + configuration.name() + " " + configuration.bearerTypeName() + " " + configuration.identifier());
  }
  textEdit->append("DEFAULT:");
  textEdit->append("\t" + network_configuration_manager->defaultConfiguration().name() + " " + network_configuration_manager->defaultConfiguration().bearerTypeName());

  textEdit->append("---------------");
  textEdit->append("Network Session");
  textEdit->append("---------------");
  network_session = new QNetworkSession(network_configuration_manager->defaultConfiguration(), this);
  network_session->open();
  if(network_session->isOpen()){
    textEdit->append("\tisOpen");
  }else{
    textEdit->append("\tnoOpen");
  }
  textEdit->append("---------------");
  textEdit->append("Network Manager");
  textEdit->append("---------------");
  QNetworkAccessManager *network_access_manager = new QNetworkAccessManager(this);
  reply = network_access_manager->get(QNetworkRequest(QUrl("http://google.com")));
  QObject::connect(reply, SIGNAL(finished()), this, SLOT(reply_finished()));
}

void CheckNetwork::reply_finished(){
  textEdit->append("REPLY: " + QString::number(reply->bytesAvailable()) + " byte(s)");
  textEdit->append("REPLY DATAS: \n\n" + reply->readAll());
}

 

The results show all up and running network interfaces, the active network configurations and the default network configuration. The important information got is that the default network configuration is right my local LAN network interface, which denies my suspicion.

But why it cannot load urls successfully? Accidentally, I find a line of code in my program:

QNetworkProxyFactory::setUseSystemConfiguration(true);

I am actually not clear about what this code does. After doing some research on the class  QNetworkProxyFactory and the function setUseSystemConfiguration, I am aware that something wrong may be with this code. This function tells qt that when loading a url, please use system specified proxy. The proxy settings are got from the settings in IE(which I rarely used). Then I run IE, open its tools/Internet options/connection/LAN settings/, and find “use proxy server for LAN” option is checked. The proxy server is set to 127.0.0.1, which is certainly not correct. I also find a hint message here: “not apply to dial-up/vpn connections”, which explains why my program can work on VPN connections.

Now the root cause of this problem is found and the solution is straightforward: check off the proxy server option.

Posted in

Comments are closed, but trackbacks and pingbacks are open.