MSDN states,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;
}
答案 0 :(得分:0)
好吧,看起来你是对的。事实上,我没有看到任何迹象表明WinINet支持分块上传; only WinHTTP does。查看Wine source code(往往非常准确),“Content-Length”标题始终附加到“GET”以外的任何方法的请求中。
根据HTTP specs,如果存在非身份“Transfer-Encoding”标头,则认为该消息被分块。如果还存在“Content-Length”标题,则必须忽略它。如果虚假的“Content-Length:0”标题导致您出现问题,那么可能会认为这是服务器的问题。我会试试WinHTTP,看看它是否解决了这个问题。
答案 1 :(得分:0)
很好的问题,我在很长一段时间内对此感到沮丧。
你对HTTP_ADDREQ_FLAG_REPLACE
的直觉证明是正确的......但是,你必须很晚才打电话。具体而言,您需要register a status callback,并且当您使用INTERNET_STATUS_CONNECTED_TO_SERVER
调用回调时, 是您删除标题的时候:
// Inside InternetStatusCallback
//
if (dwInternetStatus == INTERNET_STATUS_CONNECTED_TO_SERVER)
{
HttpAddRequestHeaders(Handle, L"Content-Length", -1, HTTP_ADDREQ_FLAG_REPLACE);
}