wininet https connections not persisting with stunnel

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a simple wininet test app that connects to a server and executes 4 https GETs. It works fine on one server, but on another server I can see via both WireShark and the WinInet event viewer that the connections are not being reused from the pool.
Instead, a new connection is established for each request and there are 4 ssl handshakes.
The server that works is running IIS, and the server for which the connections fail to persist is running stunnel.
However I have seen https connections from IE to the stunnel server persist just fine. Here is my code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // wininet.cpp : Defines the entry point for the console application.
<span style="color:Green; //

#include <span style="color:#A31515; "stdafx.h"

<span style="color:Blue; char * acceptTypes[] = {<span style="color:#A31515; "*/*", 0};
<span style="color:Blue; bool secure = <span style="color:Blue; false;
<span style="color:Blue; bool ignorecerterrs = <span style="color:Blue; false;
<span style="color:Blue; char * server = NULL, * file = NULL;

<span style="color:Blue; void PostARequest(HANDLE hConnect)
{
<span style="color:Blue; int rflags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_HYPERLINK;

<span style="color:Blue; if (secure)
rflags |= INTERNET_FLAG_SECURE;

HANDLE hRequest = HttpOpenRequest(hConnect, <span style="color:#A31515; "GET", file, NULL, <span style="color:#A31515; "TestClient", (LPCTSTR *) acceptTypes, rflags, 0);

HttpSendRequest(hRequest, NULL, -1L, NULL, 0);

<span style="color:Blue; long contentlen = 0;
DWORD len = <span style="color:Blue; sizeof(<span style="color:Blue; long);

HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER, &contentlen, &len, NULL);

DWORD nread = 0;
<span style="color:Blue; char buf[1024];
<span style="color:Blue; for (<span style="color:Blue; int ttlread = 0; ttlread < contentlen; ttlread += nread)
InternetReadFile(hRequest, buf, <span style="color:Blue; sizeof(buf), &nread);

printf(<span style="color:#A31515; "READ %d ", contentlen);
}

<span style="color:Blue; int _tmain(<span style="color:Blue; int argc, _TCHAR* argv[])
{
server = argv[1];
file = argv[2];
secure = (argc > 3 && argv[3][0] == s);

HANDLE hInternet = InternetOpen(<span style="color:#A31515; "TestClient", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HANDLE hConnect = InternetConnect(hInternet, server, secure ? 443 : 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

<span style="color:Blue; for (<span style="color:Blue; int n = 0; n < 4; n++)
PostARequest(hConnect);

InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);

<span style="color:Blue; return 0;
}


[/code]
What factors determine whether wininet reuses a connection from the keep-alive pool vs. creating a new connection?


View the full article
 
Back
Top