如何用HttpSendRequest函数post一个大文件到web

ICanSeeIt 2005-09-19 02:27:01
我通过HttpSendRequest(hRequest, hdrs, strlen(hdrs), postdata, strlen(postdata))将一块数据提交给CGI,如果postdata比较小的话没有问题,可是当我的postdata比较大,比如几十k的时候服务器就会报如下的错:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Size of a request header field exceeds server limit.<P>
。。。。
。。。。
请问如何解决
...全文
342 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
duyhui 2005-09-27
HttpSendRequest只能发送小文件,大文件就不行了.发送大文件应该用HttpSendRequestEx
楼主看看微软的例子

http://support.microsoft.com/kb/177188/EN-US/
回复
masterz 2005-09-23
FROM MSDN:
Please note that HttpSendRequest can be used only with a single buffer of limited size. HttpSendRequestEx and InternetWriteFile can be used to send buffers of any size as follows:

BOOL UseHttpSendReqEx(HINTERNET hConnect, TCHAR *upFile)
{
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
"/test/page.htm", NULL, NULL, NULL, 0, 0);
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
}

HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", upFile);
return FALSE;
}

BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal );

if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
return FALSE;
}

DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ;

CloseHandle (hFile);
printf ("Actual written bytes: %d\n", sum);

if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
return TRUE;
}

回复
相关推荐
发帖
HTML/XML
创建于2007-09-28

3054

社区成员

VC/MFC HTML/XML
申请成为版主
帖子事件
创建了帖子
2005-09-19 02:27
社区公告
暂无公告