Nilesh Shinde
Nilesh Shinde

Reputation: 392

Authorization response of TIdHTTP->Connect() to get session ID

I am using the TIdHTTP component to connect to my server. I am using the following code for that (this is not complete code, I have removed unnecessary code lines). With the below code, I can connect to the server, and I can send a GET request to it.

My query is while authorization (in the Connect call), server sends a sessionID, which is required while making a GET request. How can I get that sessionID once the http->Connect() call is successful?

If I use a GET request for authorization, then I get the session ID using a Cookie manager. But I am interested if I can get that using a Connect() call.

 TIdHTTP* http = new TIdHTTP();
 /// Code to set IO handler and setting custom headers
 http->Connect(ipAddress,port);

 if(http->Connected())
 {
    http->Get(url,memStream);
 }

Upvotes: 0

Views: 244

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597540

The answer to your question is - NO, you cannot get a session ID from Connect(), because getting a session ID requires you to first make an HTTP request so the server can send a response, and Connect() does not send any request, it merely opens the underlying socket connection to the server's listening port. The actual request is not sent until you call the TIdHTTP::Get() method (or any other request method - Post(), Put(), Delete(), etc).

Besides, you are not supposed to be calling the TIdHTTP::Connect() method manually at all. HTTP is a stateless protocol, there is no guarantee (or need) for a TCP connection to be persisted between multiple requests to the same server. That is negotiated between client and server on a per-request basis, and TIdHTTP handles that internally, (re)connecting as needed for each request sent. All you need to do is supply a URL to Get() (or whichever request you need), and then TIdHTTP parses it to extract the host:port information and manage the socket connection accordingly.

If the server requires HTTP-level authentication (as opposed to webform-based authentication), it will ask for that in a response, which TIdHTTP handles if you have the hoProcessAuth flag enabled in the TOdHTTP::HTTPOptions property, thus triggering the TIdHTTP::On(Select)Authorization events as needed, so it can then retry the request with the authentication info added. There are also Username and Password subproperties available in the TIdHTTP::Request property.

Upvotes: 1

Related Questions