Home > Software engineering >  HttpEndRequest (hRequest, NULL, 0, 0) returns ERROR_INTERNET_FORCE_RETRY
HttpEndRequest (hRequest, NULL, 0, 0) returns ERROR_INTERNET_FORCE_RETRY

Time:10-02

 BOOL UseHttpSendReqEx (HINTERNET hConnect, TCHAR * upFile, TCHAR * localFile) 
{
INTERNET_BUFFERS BufferIn={0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer [302480].//Read from the file in chunks of 300 m, the largest support 300 MB file
BOOL bRead, bRet;
BufferIn. DwStructSize=sizeof (INTERNET_BUFFERS);
//use the put way to upload files:
Aa:
HINTERNET hRequest=HttpOpenRequest (hConnect, "PUT",
LocalFile, "HTTP/1.1", NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
if (! HRequest)
{
Printf (" Failed to open the request handle: % lu/n ", GetLastError ());
Return FALSE;
}
//open the specified file:
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 the local file % s ", upFile);
Return FALSE;
}
BufferIn. DwBufferTotal=GetFileSize (hFile, NULL);
Printf (" the File size is % d/n ", BufferIn. DwBufferTotal);
if (! HttpSendRequestEx (hRequest, & amp; 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), & amp; DwBytesRead, NULL)))
{
Printf ("/nReadFile failed on buffer % lu. The ", GetLastError ());
break;
}
if (! (bRet=InternetWriteFile (hRequest, pBuffer, dwBytesRead, & amp; DwBytesWritten)))
{
Printf ("/nInternetWriteFile failed % lu ", GetLastError ());
break;
}
The sum +=dwBytesWritten;
} while (dwBytesRead==sizeof (pBuffer));
The CloseHandle (hFile);
Printf (" Actual written bytes: % d/nupload % s successed!/n ", sum, localFile);
//end an HTTP request:
if (! HttpEndRequest (hRequest, NULL, 0, 0))
{
If (ERROR_INTERNET_FORCE_RETRY==GetLastError ())
Goto aa;
}
//if (!)
//{
//char error [256]={0};
//printf (" Error on HttpEndRequest % lu/n, ");
////return FALSE.
//}
return TRUE;
}

Do HTTP post, upload file download code from the Internet, a test is always HttpEndRequest return wrong ERROR_INTERNET_FORCE_RETRY (must run again the operation), what is the reason, the code Posting said good,

CodePudding user response:

http://www.cnblogs.com/huqingyu/archive/2009/03/04/1403472.html ?

CodePudding user response:

https://www.codeproject.com/Articles/3849/Simple-HTTP-Client-using-WININET