精华内容
下载资源
问答
  • Qt同步方式进行http请求

    千次阅读 2014-03-14 13:26:54
    这几天在做基于Qt的remeber the milk应用,刚开始为了简单,没有将实现rtm api的类继承自QObject,这样就没法通过在类中添加槽函数,来连接QNetworkAccessManager的信号来判断是否已经下载。所以要找一种阻塞等待的...


    这几天在做基于Qt的remeber the milk应用,刚开始为了简单,没有将实现rtm api的类继承自QObject,这样就没法通过在类中添加槽函数,来连接QNetworkAccessManager的信号来判断是否已经下载。所以要找一种阻塞等待的方式。

    首先尝试了QNetworkReply中的isFinished()函数,通过while循环判断reply是否已经结束,结束后再调用readAll函数读取http响应信息。结果使程序进入了无限循环,导致应用没有响应。而且奇怪的是在无限循环中间,用wireshark抓包,没有任何相关的http包。将isFinished()函数改成isRunning()来判断,结果还是一样。
    然后发现QNetworkReply是继承自QIODevice的,就试了下这个类定义的waitForReadyRead方法,结果是完全没有阻塞,直接返回了。google了下说QNetworkReply没有实现这个函数,不过没有去确认过。
    最后找到了一种使用QEventLoop来阻塞运行,直到信号发出:
    QNetworkReply *reply = _manager->post(QNetworkRequest(QUrl(SERVER_URL)), data);
    QByteArray responseData;
    QEventLoop eventLoop;
    connect(_manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    eventLoop.exec();       //block until finish
    responseData = reply->readAll();
    展开全文
  • 使用qt写的请求http服务类,实现了get同步请求、get异步请求、post同步请求、post异步请求。4个方法,方便实用。
  • qt同步方式进行http请求

    千次阅读 2014-11-21 10:34:42
    这几天在做基于Qt的remeber the milk应用,刚开始为了简单,没有将实现rtm api的类继承自QObject,这样就没法通过在类中添加槽函数,来连接QNetworkAccessManager的信号来判断是否已经下载。所以要找一种阻塞等待的...
    这几天在做基于Qt的remeber the milk应用,刚开始为了简单,没有将实现rtm api的类继承自QObject,这样就没法通过在类中添加槽函数,来连接QNetworkAccessManager的信号来判断是否已经下载。所以要找一种阻塞等待的方式。
    


    首先尝试了QNetworkReply中的isFinished()函数,通过while循环判断reply是否已经结束,结束后再调用readAll函数读取http响应信息。结果使程序进入了无限循环,导致应用没有响应。而且奇怪的是在无限循环中间,用wireshark抓包,没有任何相关的http包。将isFinished()函数改成isRunning()来判断,结果还是一样。
    然后发现QNetworkReply是继承自QIODevice的,就试了下这个类定义的waitForReadyRead方法,结果是完全没有阻塞,直接返回了。google了下说QNetworkReply没有实现这个函数,不过没有去确认过。
    最后找到了一种使用QEventLoop来阻塞运行,直到信号发出:
    QNetworkReply *reply = _manager->post(QNetworkRequest(QUrl(SERVER_URL)), data);
    QByteArray responseData;
    QEventLoop eventLoop;
    connect(_manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    eventLoop.exec();       //block until finish
    responseData = reply->readAll();


    http://coolex.info/blog/196.html
    展开全文
  • Qt HTTP请求同步调用

    万次阅读 2015-03-24 21:08:57
    Qt中,进行HTTP就行现在官方提倡使用QNetworkAccessManager,其和QNetworkRequest和QNetworkReply配合使用,来完成,其是只支持异步的操作。最近使用QML做些android的小程序,需要进行HTTPS的操作,开始是使用的JS...

    在Qt中,进行HTTP就行现在官方提倡使用QNetworkAccessManager,其和QNetworkRequest和QNetworkReply配合使用,来完成,其是只支持异步的操作。最近使用QML做些android的小程序,需要进行HTTPS的操作,开始是使用的JS中的XMLHttpRequest,其不能使用HTTPS,我很郁闷,那只有使用C++来完成,但是又发现只支持异步,但是我希望在进行网络操作时,不要响应客户的响应,给我提示,那么如果是异步,那么请求本身就需要能够标识其操作,我想如果是同步该多好啊。
    然后就有了下面的代码

    const int TIMEOUT = (30 * 1000);
    QString Supporter::httpsPostHelp(const QString &url, const QString &data)
    {
        QString _result;
        QNetworkRequest _request;
        _request.setUrl(QUrl(url));
        QSslConfiguration _sslCon = _request.sslConfiguration();
        _sslCon.setPeerVerifyMode(QSslSocket::VerifyNone);
        _request.setSslConfiguration(_sslCon);
        _request.setHeader(QNetworkRequest::ContentTypeHeader,
                           QString("application/x-www-form-urlencoded"));
    
        QNetworkReply *_reply = m_NtwkAccManager.post(_request, data.toLatin1());
        _reply->ignoreSslErrors();
    
    
        QTime _t;
        _t.start();
    
        bool _timeout = false;
    
        while (!_reply->isFinished()) {
            QApplication::processEvents();
            if (_t.elapsed() >= TIMEOUT) {
                _timeout = true;
                break;
            }
        }
    
        if (!_timeout && _reply->error() == QNetworkReply::NoError) {
            _result = _reply->readAll();
        }
    
        _reply->deleteLater();
    
        return _result;
    
    }

    开始的时候我没有加 QApplication::processEvents() 导致了事件阻塞在循环这边,那也就拿不到网络返回的数据,其实网络操作还是异步的,我们只是在一个循环中处理时间并等待,这样可以做到同步和程序正常的事件循环。

    展开全文
  • Qt同步框架

    千次阅读 2009-12-28 21:09:00
    http://labs.trolltech.com/blogs/2007/03/08/making-asynchronous-function-calls-with-qfuture/QtConcurrent::run() runs a function in a worker thread. It returns a QFuture, which is then used to syn

    http://labs.trolltech.com/blogs/2007/03/08/making-asynchronous-function-calls-with-qfuture/

    QtConcurrent::run() runs a function in a worker thread. It returns a QFuture, which is then used to synchronize with the result:

    QString foo();
    QFuture<QString> f = QtConcurrent::run(foo);
    ...
    QString string = f.result()
    

    Calling f.result() will block the current thread until foo() has returned. The QFuture template argument must match the return type of foo().

    If the function you want to run takes arguments, use QtConcurrent::bind to supply values for them:

    QString foo(const QString &string);
    QFuture<QString> f = QtConcurrent::run(QtConcurrent::bind(foo, QLatin1String("Hello World")));
    
    qDebug() < < f.result();
    

    (bind is based on the excellent boost::bind package.)

    It’s usually not a good idea to block the GUI thread to wait for results, so when writing GUI applications it’s possible to use signals and slots instead. The QFutureWatcher class is used to make the connections:

    QFutureWatcher *watcher  = new QFutureWatcher();
    QObject::connect(watcher, SIGNAL(resultReady(const QVariant&)), myObject, SLOT(handleResult(const QVariant &)));
    
    QString foo();
    QFuture<QString> f = QtConcurrent::run(foo);
    watcher->setFuture(f);
    

    When foo() returns, QFutureWatcher calls the handleResult slot using a queued signal-slot connection.

     

    MapReduce was originally developed by Google to simplify writing parallel algorithms for computer clusters. The basic idea is that you divide your algorithm into two parts: one part that can be run in parallel on individual pieces of the input data (’map’), and one sequential part that collects the map results and produces the final result (’reduce’). Your program then sends the map and reduce functions along with your input data to the MapReduce framework which automatically parcels out the data to each cluster node and collects the results afterwards.

    MapReduce in Qt Concurrent is implemented to work on shared-memory systems, so instead of managing cluster nodes it manages threads on a single computer. This also means we can drop some of the features that the Google version has, such as fault tolerance. (we assume that processors don’t fail)

    The API looks like this:

    QFuture<T> mappedReduced(list, mapFunction, reducefunction);
    

    As an example, let’s say we want to do a word frequency count on the contents of several documents. Here the map function will count the word occurrences in each document in parallel, and the reduce function will combine the them into a final frequency count.

    The input for the function is a list of text strings that contains the documents:

    QList<QString> list;
    

    The map function takes one document and produces a hash that stores the frequency count for each word in the document. This function will be called in parallel by several threads, so it can’t have any side-effects such as modifying global data (or more accurately: any side-effcts must be thread-safe). The number of threads used will be scaled according to the number of CPU cores on the system.

    QHash<QString, int> mapFunction(const QString &document);
    

    The reduce function takes one intermediate result hash and aggregates it into the final result. Qt Concurrent will make sure only one thread calls this function at a time. This has two implications: there is no need to use a mutex lock when updating the result variable, and the system can be smarter about how it manages threads. If a thread tries to become the reducer thread while another thread is reducing, the first thread doesn’t have to block but can put the result on the to-be-reduced queue and then call the map function on a new piece of data instead.

    void reduceFunction(QHasht<QString, int> &finalResut, const QHash<QString, int> &intermediateResult);
    

    Finally we put it all together like this:

    QFuture<QHash<QString, int> >counting =  mappedReduced(list, mapFunction, reduceFunction);
    

    Since mappedReduced returns a QFuture we have several options on how to synchronize with the result. The simplest thing is to just call QFuture::result() which will block until the result is ready. If blocking is inappropriate (say we are in the gui thread) we can use signal-slot connections to get progress and result notifications instead. It’s also possible to cancel mappedReduced by calling QFuture::cancel().

    展开全文
  • QT 同步post请求

    2019-06-15 14:57:41
    request.setUrl(QUrl("http://localhost/zerg/public/index.php/insertTaskItem")); request.setRawHeader("token", "ad3286591af113263e0dfb27b5f59c5a"); QByteArray dasds("name=3dffdfs"); QNetworkReply...
  • qt同步发送请求

    2019-06-14 17:40:47
    QString HttpUtils::Post(QString url,QString json){ QNetworkAccessManager * _manager = new QNetworkAccessManager; QNetworkRequest request = QNetworkRequest(QUrl(url)); request.setHeader(QN...
  • qt同步方式发送post请求

    千次阅读 2018-05-22 11:12:46
    不成功的方式: 1. QNetworkReply的isFinished()函数, 通过while循环判断reply是否已经结束, 结束后再调用readAll()读取响应信息, 结果与判断isRunning() 方式结果一样, 都会进入死循环, 没有响应....
  • QT 同步上传文件到服务器

    千次阅读 2018-06-08 16:55:51
     ////编辑HTTP头部  QNetworkRequest qRequest;  std::string strUrl = ConfigData::GetCasData()->GetHostByHostName(SYSDCPFILE) + SYSDCPFILE_UPLOADFILE;  boost::format fmt("?offset=%d&uploadParam=%s");...
  • QT实现同步调用WebService

    千次阅读 2015-04-02 16:29:17
    QT实现同步调用WebService
  • /// 此类作为和使用http连接的server进行交互客户端 /// </summary> #include <QObject> class QJsonObject; class QTimerEvent; class QJsonObject; class MTHttpClientPrivate; class MTHttpClient :...
  • 关于Http HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。 HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件,...
  • Qt实现HTTP客户端操作

    万次阅读 多人点赞 2019-07-27 23:01:22
    本文是QtHTTP相关接口一个简单总结,主要是get/post请求以及表单提交等应用,HTTP相关知识自行百度。 目录 1.简介 2.认识QNetworkAccessManager 3.服务请求QNetworkRequest 4.服务响应QNetworkReply 5.表单...
  • qt 自动升级程序 通过HTTP同步下载

    热门讨论 2009-12-18 17:12:36
    qt 自动升级程序编写 读取XML 通过HTTP下载文件 下载文件通过MD5验证判断文件是否下载完全
  • QT 通过QNetworkAccessManager进行http请求的超时与同步异步处理 QNetworkAccessManager是QT的网络包,可以设置超时,见下面代码。 QReplyTimeout *pTimeout = new QReplyTimeout(reply, 2000); // 超时进一步...
  • QT中对HttpRequest的实现是利用了QT的singal-slot实现的异步请求,虽然有时这很有用,比如节约时间,不会使UI卡住等,但有时,我们还是需要阻塞式的同步HTTP请求,因为在这个HTTP请求返回之前是不能继续工作的,比如登录等...
  • Qt linux文件同步写入

    2017-07-17 18:12:00
    因为linux 系统机制问题,文件的创建和写入并不会直接写入硬盘。...我们能够在Qt程序中调用该命令。 附:sync 命令说明 sync命令 linux同步数据命令 格式: sync  用途:更新 i-node 表,并将...
  • Qt:局域网文件同步工具

    千次阅读 2015-01-16 19:12:35
    Qt:局域网文件同步工具
  • Qt音视频开发24-ffmpeg音视频同步

    千次阅读 2020-09-24 11:18:48
    用ffmpeg来做音视频同步,个人认为这个是ffmpeg基础处理中最难的一个,无数人就卡在这里,怎么也不准,本人也是尝试过网上各种demo,基本上都是渣渣,要么仅仅支持极其少量的视频文件比如收到的数据包是一帧视频一帧...
  • qthttp请求demo

    2019-04-07 12:00:59
    修改了 一下 支持 混合表单post上传 get 同步请求 的demo 亲测可用
  • Qt的网络操作类是异步(非阻塞的),但有时想做一些阻塞的事情就不方便了,可用如下几行代码轻松实现: 1 QByteArray MyNetworkAccess::get(const QString &strUrl) 2 { 3 assert(!strUrl.isEmpty());...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 10,189
精华内容 4,075
关键字:

qt同步http