2

要将文件发送到服务器,我在 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);

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

所以我有两个问题:

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

1 回答 1

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);
    
于 2012-12-07T00:54:07.567 回答