2

To send files to server, I do a HTTPS put request in Windows, which looks like this:

hSession = WinHttpOpen(  L"Agent/1.0",..
hConnect = WinHttpConnect(hSession,..
hRequest = WinHttpOpenRequest( hConnect, L"PUT",..
WinHttpSetCredentials(hRequest,..
WinHttpAddRequestHeaders( hRequest,..
WinHttpSendRequest( hRequest,..
WinHttpWriteData(hRequest,..
WinHttpReceiveResponse(hRequest,..
WinHttpQueryHeaders(hRequest,..
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);

This pack of command is run for every file, which should be sent to server. Establishing the connection from scratch for every file to be sent creates additional overhead. Now I'm looking for a way to reduce this overhead.

So I have two questions:

  • Is it necessary to open and close a new HTTPS connection for each put request?
  • Is there a way to establish a session and reuse the same HTTPS connection for many put requests within this session?
Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40
Anton K
  • 4,658
  • 2
  • 47
  • 60
  • 2
    All you have to do is stop closing session and connection. WinHTTP (assuming version 5.1) handles all this internally, managing a pool of persistent TCP connection and letting requests "borrow" a connection only while they are actually sending a request and receiving a response. – David Schwartz Dec 03 '12 at 20:06
  • @David Schwartz, did I get you correctly? I can go like this: hSession = WinHttpOpen( L"Agent/1.0",.. hConnect = WinHttpConnect(hSession,.. for (...) { hRequest = WinHttpOpenRequest( hConnect, L"PUT",.. WinHttpSetCredentials(hRequest,.. WinHttpAddRequestHeaders( hRequest,.. WinHttpSendRequest( hRequest,.. WinHttpWriteData(hRequest,.. WinHttpReceiveResponse(hRequest,.. WinHttpQueryHeaders(hRequest,.. } if (hRequest) WinHttpCloseHandle(hRequest); if (hConnect) WinHttpCloseHandle(hConnect); if (hSession) WinHttpCloseHandle(hSession); – Anton K Dec 03 '12 at 21:26
  • 1
    Yes. You can keep issuing as many requests as you want against the same connection on the same session. – David Schwartz Dec 03 '12 at 21:34
  • @DavidSchwartz Why don't you put your comment as answer? – Anton K Dec 06 '12 at 20:14

1 Answers1

2

The answers are the following:

  1. No. One connection may perform a bunch of requests.
  2. This is the draft of the code:

    hSession = WinHttpOpen( L"Agent/1.0",..
    hConnect = WinHttpConnect(hSession,.. 
    for (all_files_to_upload) { 
        hRequest = WinHttpOpenRequest( hConnect, L"PUT",..
        WinHttpSetCredentials(hRequest,.. 
        WinHttpAddRequestHeaders( hRequest,.. 
        WinHttpSendRequest( hRequest,.. 
        WinHttpWriteData(hRequest,.. 
        WinHttpReceiveResponse(hRequest,.. 
        WinHttpQueryHeaders(hRequest,.. 
        WinHttpCloseHandle(hRequest);
    } 
    
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);
    
Anton K
  • 4,658
  • 2
  • 47
  • 60