CHttpFile重复出现或多次调用SendRequest()/ Read()将给出过时的数据

时间:2018-08-09 12:01:33

标签: rest http mfc

如标题中所述,我在循环中以连续顺序执行CHttpFile :: SendRequest()和Read()以通过Web服务器填充和检索数据。

QueryInfoStatusCode()不会报告任何错误,并且网络服务器将以预期的方式存储数据。

但是,Read()方法始终返回相同的字符串,应该是对已处理数据的答案。

问题:我在这里想念什么?我必须以任何方式清除CHttpFile的readbuffer吗?这可能是Web服务器实施中的缺陷吗?

INTERNET_FLAG_DONT_CACHE-标志已准备好提供给CInternetSession()和OpenRequest()调用...

如果我在循环中调用CHttpConnection :: OpenRequest()/ CHttpFile :: Close(),一切正常,但非常耗时...

    CInternetSession inetSession( "me", 1, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_DONT_CACHE);

    CHttpConnection* pInetHttpConnection = 
        inetSession.GetHttpConnection( "awebserver.com", INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, INTERNET_DEFAULT_HTTPS_PORT, "", "");

    CHttpFile* pHttpFile = pInetHttpConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
        "/a/service/register", NULL, 1, NULL, NULL,
        INTERNET_FLAG_EXISTING_CONNECT | 
        INTERNET_FLAG_SECURE |
        INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
        INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);

    pHttpFile->AddRequestHeaders( "content-type=application/json;charset=UTF-8");

    for( int counter = 0; counter != 10; counter++)
    {
        // build the cargo ...
        //-----------------------
        CString data; 
        data.Format( "{ \"value\" : %d }", counter);

        pHttpFile->SendRequest( NULL, 0, (LPVOID)(LPCTSTR) data, data.GetLength());

        DWORD dwRet;
        pHttpFile->QueryInfoStatusCode( dwRet);
        ASSERT( dwRet == 200);

        // read the answer ...
        //--------------------------
        char szInfoBuffer[ 4096]; 

        DWORD dwFileSize;   
        DWORD dwInfoBufferSize = sizeof( szInfoBuffer);

        pHttpFile->QueryInfo( HTTP_QUERY_CONTENT_LENGTH, (void*) szInfoBuffer, &dwInfoBufferSize,NULL);  // always returns the same (correct) size, does not grow eventualy

        CString result;
        LPTSTR pBuffer = result.GetBufferSetLength( dwFileSize);

        UINT nRead = pHttpFile->Read( pBuffer, dwFileSize);
        while(nRead > 0)
        {
            pBuffer += nRead; dwFileSize -= nRead;
            nRead = pHttpFile->Read( pBuffer, dwFileSize);
        }
        result.Releasebuffer();

        TRACE( result); // Always returns { "stored" : 0 } instead of { "stored" : 1 }, { "stored" : 2 }, { "stored" : 3 } etc.
    }

0 个答案:

没有答案