当前位置: 首页>>代码示例>>C++>>正文


C++ QNetworkSession::errorString方法代码示例

本文整理汇总了C++中QNetworkSession::errorString方法的典型用法代码示例。如果您正苦于以下问题:C++ QNetworkSession::errorString方法的具体用法?C++ QNetworkSession::errorString怎么用?C++ QNetworkSession::errorString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QNetworkSession的用法示例。


在下文中一共展示了QNetworkSession::errorString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char**argv)
{
    QCoreApplication app(argc, argv);

#ifdef Q_OS_SYMBIAN
    QNetworkConfigurationManager configurationManager;
    QNetworkConfiguration configuration = configurationManager.defaultConfiguration();
    if (!configuration.isValid()) {
        qDebug() << "Got an invalid session configuration";
        exit(1);
    }

    qDebug() << "Opening session...";
    QNetworkSession *session = new QNetworkSession(configuration);

    // Does not work:
//    session->open();
//    session->waitForOpened();

    // works:
    QEventLoop loop;
    QObject::connect(session, SIGNAL(opened()), &loop, SLOT(quit()), Qt::QueuedConnection);
    QMetaObject::invokeMethod(session, "open", Qt::QueuedConnection);
    loop.exec();


    if (session->isOpen()) {
        qDebug() << "session opened";
    } else {
        qDebug() << "session could not be opened -" << session->errorString();
        exit(1);
    }
#endif

    // create it
    QAbstractSocketEngine *socketEngine =
            QAbstractSocketEngine::createSocketEngine(QAbstractSocket::TcpSocket, QNetworkProxy(QNetworkProxy::NoProxy), 0);
    if (!socketEngine) {
        qDebug() << "could not create engine";
        exit(1);
    }

    // initialize it
    bool initialized = socketEngine->initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol);
    if (!initialized) {
        qDebug() << "not able to initialize engine";
        exit(1);
    }

    // wait for connected
    int r = socketEngine->connectToHost(QHostAddress("74.125.77.99"), 80); // google
    bool readyToRead = false;
    bool readyToWrite = false;
    socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, true, 10*1000);
    if (r <= 0) //timeout or error
        exit(1);
    if (readyToWrite) {
        // write the request
        QByteArray request("GET /robots.txt HTTP/1.0\r\n\r\n");
        int ret = socketEngine->write(request.constData(), request.length());
        if (ret == request.length()) {
            // read the response in a loop
            do {
                bool waitReadResult = socketEngine->waitForRead(10*1000);
                int available = socketEngine->bytesAvailable();
                if (waitReadResult == true && available == 0) {
                    // disconnected
                    exit(0);
                }
                bzero(buf, bufsize);
                ret = socketEngine->read(buf, available);
                if (ret > 0) {
#ifdef Q_OS_SYMBIAN
                    qDebug() << buf; //printf goes only to screen, this goes to remote debug channel
#else
                    printf("%s", buf);
#endif
                } else {
                    // some failure when reading
                    exit(1);
                }
            } while (1);
        } else {
            qDebug() << "failed writing";
        }
    } else {
        qDebug() << "failed connecting";
    }
    delete socketEngine;
}
开发者ID:dewhisna,项目名称:emscripten-qt,代码行数:90,代码来源:main.cpp


注:本文中的QNetworkSession::errorString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。