Qt 和 HTTP 下载

发布于 2024-09-24 19:30:33 字数 211 浏览 0 评论 0原文

我正在开发一个类,它将实现某个即时消息协议的客户端。
我需要首先发出一个 HTTP 请求,然后读取数据并使用从第一个请求接收到的已处理数据发出另一个 HTTP 请求,只有在第二个请求之后我才能打开到服务器的 tcp 套接字。 执行此操作的最佳方法(最佳 QT 方法?)是什么,因为我还没有找到阻止 HTTP 请求结束的方法。我正在考虑在这部分使用 libcurl,但仅为此使用另一个库似乎有点过分了。

I am working on a class that will implement a client for a certain Instant Messaging protocol.
I need to first make a HTTP request, then read the data and make another HTTP request with the processed data received from the first request and only after the second request i can open a tcp socket to the server.
What is the best way(best QT way?) to go about doing this as i haven't found a way to block until HTTP request is over. I was considering using libcurl for this part but it seems a overkill to use another lib only for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-10-01 19:30:33

QHttp 将发送一组信号来通知请求的状态。例如,您可以将 requestFinished() 信号连接到一个插槽,该插槽将处理该信号并启动第二个请求。

或者作为另一种解决方案,

//possibly in constructor
connect(myHttp, SIGNAL(requestFinished(int, bool)), 
        this, SLOT(requestHandler(int, bool)))


//first call somewhere
firstReqId = myHttp->get("first.com", buff);

void requestHandler(int id, bool error)
{
     if (error)
        panic();
     if(id == firstReqId) {
         process(buff);
         secondReqId = myHttp->get("second.com", buff2);
     }
     if (id == secondReqId) {
         process(buff2);
         sock.connectToHost("server.com", "5222"); //etc
     }
}

您可以巧妙地使用锁定结构,例如 QMutex 并通过实现您自己的阻塞请求方法来包装 QHttp,这将一次允许一个请求。但我认为就你的情况而言,第一种方法更好。

QHttp will send a set of signals to notify the state of a request. You may for example connect requestFinished() signal to a slot that will process it and start your second request.

Idea in pseudo-qt

//possibly in constructor
connect(myHttp, SIGNAL(requestFinished(int, bool)), 
        this, SLOT(requestHandler(int, bool)))


//first call somewhere
firstReqId = myHttp->get("first.com", buff);

void requestHandler(int id, bool error)
{
     if (error)
        panic();
     if(id == firstReqId) {
         process(buff);
         secondReqId = myHttp->get("second.com", buff2);
     }
     if (id == secondReqId) {
         process(buff2);
         sock.connectToHost("server.com", "5222"); //etc
     }
}

Or as another solution you can smartly use locking structures such as QMutex and wrap QHttp by implementing your own blocking request method, that will allow one request at time. But I think that in your case the first approach is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文