如何在会话中为许多put请求重用相同的HTTPS连接?

时间:2012-12-03 20:00:16

标签: c++ web https put winhttp

要将文件发送到服务器,我在Windows中执行HTTPS put请求,如下所示:

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);

为每个文件运行此命令包,应将其发送到服务器。从头开始为每个要发送的文件建立连接会产生额外的开销。现在我正在寻找一种减少这种开销的方法。

所以我有两个问题:

  • 是否有必要为每个投放请求打开和关闭新的HTTPS连接?
  • 有没有办法在此会话中为多个put请求建立会话并重用相同的HTTPS连接?

1 个答案:

答案 0 :(得分:2)

答案如下:

  1. 否。一个连接可以执行一堆请求。
  2. 这是代码草案:

    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);