本人有以下代码bool MyTest(char* upFile, char* serverip, char* fileinserver)
{ HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, serverip, 80,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
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, "POST",
fileinserver, 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;}函数执行成功,可是服务器端就是没有文件?
谁有成功的代码?????????????????????????????????????????????
{ HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, serverip, 80,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
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, "POST",
fileinserver, 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;}函数执行成功,可是服务器端就是没有文件?
谁有成功的代码?????????????????????????????????????????????
TCHAR szHeaders[] = _T("Accept: */*\r\nContent-Type: multipart/form-data; boundary=----VisualKingBoundary----\r\n");
char szMimeHeader[] = "----VisualKingBoundary----\r\nContent-Disposition: form-data; name=\"userfile\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n";
char szMimeTail[] = "\r\n----VisualKingBoundary----\r\n";
TCHAR szContentHeader[] = _T("Content-Length: %d");UINT UseHttpSendReqEx(HINTERNET hRequest, LPCTSTR szHead, LPCTSTR szFileName)
{
UINT nRet = INTERNET_ERROR_OK; INTERNET_BUFFERS BufferIn; FILE * pFile; if ( !(pFile = ::_tfopen (szFileName, _T("rb") ) ) )
{
nRet = INTERNET_ERROR_FILEOPEN;
goto exit_point;
}
else
{
VOID* szTemp[10240]; DWORD dwFileSize = (DWORD)_filelength(_fileno(pFile)); int nHeadSize = 0;
if (szHead)
nHeadSize = (int)_tcslen (szHead); ::HttpAddRequestHeaders(hRequest,szHead,nHeadSize,HTTP_ADDREQ_FLAG_ADD);
::HttpAddRequestHeaders(hRequest,szHeaders,(DWORD)_tcslen(szHeaders),HTTP_ADDREQ_FLAG_ADD); char pTmp[1025] = {0};
#ifdef _UNICODE
USES_CONVERSION;
sprintf(pTmp,szMimeHeader,W2A(szFileName));
#else
sprintf(pTmp,szMimeHeader,szFileName);
#endif DWORD dwMimeHeader = (DWORD)strlen(pTmp);
DWORD dwMimeTail = (DWORD)strlen(szMimeTail);
DWORD dwBufferTotal = dwMimeHeader + dwFileSize + dwMimeTail; CString strContentHead;
strContentHead.Format(szContentHeader,dwBufferTotal);
::HttpAddRequestHeaders(hRequest,strContentHead,(DWORD)_tcslen(strContentHead),HTTP_ADDREQ_FLAG_ADD); BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;//szHeaders; // 请求头
BufferIn.dwHeadersLength = 0;//strlen(szHeaders);
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwBufferTotal; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0; if (!HttpSendRequestEx(hRequest, NULL, NULL, 0, 0))
{
m_dwError = ::GetLastError();
nRet = INTERNET_ERROR_CONNECT;
goto exit_point;
}
else
{
// 检测有没有数据读取
//DWORD dwAvailable = 0;
//if (InternetQueryDataAvailable(hRequest,&dwAvailable,0,0))
//{
// dwAvailable = dwAvailable;
//}
//m_dwError = ::GetLastError(); // 上传文件
DWORD dwTotalUp = 0;
DWORD dwFront = dwMimeHeader + dwFileSize;
do
{
DWORD dwBytesWritten = 0;
int nSent = 0;
LPVOID pBuf = NULL;
int dwBytes = 1024; if (dwTotalUp == 0) // 发送Mime头
{
nSent = dwMimeHeader;
pBuf = (LPVOID)pTmp;
}
else if (dwTotalUp < dwFront) // 发送文件内容
{
nSent = (int)fread(szTemp, sizeof (char), dwBytes , pFile);
pBuf = szTemp;
}
else // 发送Mime尾
{
nSent = dwMimeTail;
pBuf = (LPVOID)szMimeTail;
} if (nSent)
{
if (!::InternetWriteFile(hRequest, pBuf, (DWORD)nSent, &dwBytesWritten))
{
m_dwError = ::GetLastError();
nRet = INTERNET_ERROR_WRITEFILE;
break;
}
else
{
dwTotalUp += dwBytesWritten;
}
} if (dwTotalUp > dwFront)
{
break;
}
}while (true);
if (!HttpEndRequest(hRequest, NULL, 0, 0))
{
m_dwError = ::GetLastError();
nRet = INTERNET_ERROR_WRITEFILE;
} if (dwBufferTotal != dwTotalUp)
nRet = INTERNET_ERROR_WRITEFILE;
}
}exit_point: if (pFile)
fclose (pFile); if (m_dwError == ERROR_INTERNET_OPERATION_CANCELLED)
{
nRet = INTERNET_ERROR_USERCANCEL;
} if (m_dwError == ERROR_INVALID_HANDLE)
{
nRet = INTERNET_ERROR_USERCANCEL;
} //if (m_dwError == ERROR_INTERNET_CONNECTION_ABORTED)
//{
// nRet = INTERNET_ERROR_USERCANCEL;
//} return nRet;
}