tags:

views:

28

answers:

1

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.

+1  A: 

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.

doc