4

MSDN声明WinInet 不支持分块上传(“客户端代码必须执行分块。”)。对我来说,这意味着我可以手动分块传输。我的意图是通过 HttpAddRequestHeaders 添加“Transfer-Encoding: chunked” ,通过HttpAddRequestHeaders HTTP_ADDREQ_FLAG_REPLACE 删除 Content-Length,然后在我的 InternetWriteFile 循环中,将数据写入分块编码块中。问题是,我似乎无法说服 WinInet 不发送 Content-Length。即使在删除之后,它最终也会向服务器发送“Content-Length: 0”(除了“Transfer-Encoding: chunked”),这会使服务器感到困惑。

我还尝试在HttpSendRequestEx中设置 HSR_CHUNKED 标志。

有没有人有让 WinInet 跳过发送 Content-Length 的示例?

我知道 WinHTTP 声称支持分块上传,但我们对 WinInet 有其他依赖项,这就是为什么我希望尽可能在那里解决问题。

这是我尝试过的代码示例:

#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <stdio.h>

bool http_putfile(HINTERNET hConnection, TCHAR* resource);

#define HOST  _T("www.website.com")
#define RESOURCE _T("/path/for/resource")

int _tmain(int argc, TCHAR* argv[])
{
    LPCTSTR lpszHostName = HOST;
    INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT;
    DWORD dwService = INTERNET_SERVICE_HTTP;
    DWORD dwFlags = NULL;
    DWORD dwContext = 0;

    HINTERNET hSession = InternetOpen(
        argv[0],
        INTERNET_OPEN_TYPE_DIRECT, 
        NULL,
        NULL,
        NULL);

    if(hSession != NULL)
    {
        HINTERNET hConnection = InternetConnect(
            hSession,
            lpszHostName,
            nServerPort,
            NULL,
            NULL,
            dwService,
            dwFlags,
            dwContext);

        if(hConnection != NULL)
        {
            http_putfile(hConnection, RESOURCE);

            InternetCloseHandle(hConnection);
        }
        else
        {
            printf("InternetConnect failed: %d\n", GetLastError());
        }

        InternetCloseHandle(hSession);
    }
    else
    {
        printf("InternetOpen failed: %d\n", GetLastError());
    }

    return 0;
}

bool http_putfile(HINTERNET hConnection, TCHAR* resource)
{
    bool result = false;

    HINTERNET hRequest = HttpOpenRequest(
        hConnection,
        _T("PUT"),
        resource,
        NULL,
        NULL,
        NULL,
        INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_CACHE_WRITE,
        0);

    if(hRequest != NULL)
    {
        HttpAddRequestHeaders(
            hRequest,
            _T("Transfer-Encoding: chunked\r\n"),
            -1L,
            HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);

        // have tried:
        // Content-Length
        // Content-Length:
        // Content-Length\r\n
        // Content-Length:\r\n
        // all with/without HTTP_ADDREQ_FLAG_ADD.  Have even tried adding in a Content-Length
        // and then removing it.  All results show "Content-Length: 0" in the header on the wire.
        if(HttpAddRequestHeaders(
            hRequest,
            _T("Content-Length"),
            -1L,
            HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE) == FALSE)
        {
            DWORD err = GetLastError();
        }

        // have tried both 0 here for flags as documented on msdn
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384318%28v=vs.85%29.aspx
        // as well as other combinations of INITIATE/CHUNKED
        if(HttpSendRequestEx(hRequest, NULL, NULL, HSR_INITIATE | HSR_CHUNKED /* 0 */, NULL))
        {
            DWORD wrote = 0;
            char* chunks = "5\r\nCHUNK0\r\n";

            if(InternetWriteFile(
                    hRequest,
                    chunks,
                    strlen(chunks),
                    &wrote) == FALSE)
            {
                printf("InternetWriteFile failed: %d\n", GetLastError());
            }

            HttpEndRequest(hRequest, NULL, 0, NULL);
        }
        else
        {
            printf("HttpSendRequestEx failed: %d\n", GetLastError);
        }

        InternetCloseHandle(hRequest);
    }
    else
    {
        printf("HttpOpenRequest failed: %d\n", GetLastError());
    }

    return result;
}
4

2 回答 2

0

很好的问题,我自己为此感到沮丧了好几个小时。

你的直觉HTTP_ADDREQ_FLAG_REPLACE被证明是正确的......但是,你必须很晚才结束。具体来说,您需要注册一个状态回调,并且当您的回调被调用时INTERNET_STATUS_CONNECTED_TO_SERVER当您删除标头时:

// Inside InternetStatusCallback
//
if (dwInternetStatus == INTERNET_STATUS_CONNECTED_TO_SERVER)
{
  HttpAddRequestHeaders(Handle, L"Content-Length", -1, HTTP_ADDREQ_FLAG_REPLACE);
}
于 2013-10-17T05:27:51.283 回答
0

Well, it looks like you are right. In fact, I don't see anything to indicate that WinINet supports chunked uploading; only WinHTTP does. Looking at the Wine source code (which tends to be quite accurate), the "Content-Length" header is ALWAYS appended to the request for any method other than "GET".

According to the HTTP specs, if a non-identity "Transfer-Encoding" header is present then the message is considered to be chunked. If a "Content-Length" header is also present then it MUST be ignored. If the spurious "Content-Length: 0" header is causing you problems then it could be argued that it is an issue with the server. I would give WinHTTP a try and see if it fixes the problem.

于 2012-04-09T20:36:44.873 回答