INTERNET_BUFFERS BufferIn; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = _T(szHttpHeader); BufferIn.dwHeadersLength = _tcslen(szHttpHeader); BufferIn.dwHeadersTotal = _tcslen(szHttpHeader); BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = dwTotalPostSize; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; if ( FALSE == ::HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0) ) { return false; }
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL; ::PostMessage( lpThread->hWnd, lpThread->uPostMsg, (WPARAM)tsStart, NULL ); hSession = InternetOpen( lpThread->szAgent, PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0 );
if (hSession)
{
hConnect = ::InternetConnect(hSession, // 当前internet会话句柄
lpThread->szUrl+7, // Server Name,去掉前面的"http://"
INTERNET_DEFAULT_HTTP_PORT, // Server Port
NULL, // User Name
NULL, // Password
INTERNET_SERVICE_HTTP, // Type of service to access
0,
0);
if (hConnect)
{
DWORD dwFlags = INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE;
hRequest =::HttpOpenRequest(hConnect,
_T("POST"), // HTTP Verb
lpThread->szUri,// Uri, Object Name
HTTP_VERSION, // Version
NULL, // Reference
NULL, // Accept Type
dwFlags,
NULL); // context call-back point
if (hRequest)
{
bRel = pPhoto->UserHttpSendReqEx(hRequest,
lpThread->szFile,
lpThread->hWnd,
lpThread->uPostMsg); ::InternetCloseHandle( hRequest );
}
::InternetCloseHandle( hConnect );
}
::InternetCloseHandle( hSession );
}
组Http包,投递给服务器.
TCHAR szHttpHeader[ DEFAULT_BUF_SIZE ];
ZeroMemory(szHttpHeader,sizeof(szHttpHeader)); _tcscat( szHttpHeader, _T("Accept: */*\r\n") );
_tcscat( szHttpHeader, _T("Content-Type: multipart/form-data; boundary=") );
_tcscat( szHttpHeader, DEFAULT_BOUNDRY_TAG );
_tcscat( szHttpHeader, _T("\r\n") );
_tcscat( szHttpHeader, _T("Cookie: "));
_tcscat( szHttpHeader, _T("你的Cookie\r\n"));
_tcscat( szHttpHeader, _T("\r\n\r\n") );
INTERNET_BUFFERS BufferIn; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = _T(szHttpHeader);
BufferIn.dwHeadersLength = _tcslen(szHttpHeader);
BufferIn.dwHeadersTotal = _tcslen(szHttpHeader);
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwTotalPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0; if ( FALSE == ::HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0) )
{
return false;
}
下面的代码实现了这个功能,比上面的简单。你可以劫包看看http的头是否带cookie了,然后
检查服务器返回的信息是否正确
/*
---------------------------------------------------------------
获得http请求数据
---------------------------------------------------------------
*/
bool CUploadPhoto::GetHttpRequest(LPCTSTR lpszUrl, LPTSTR lpszBuffer)
{
bool bRel = false;
HINTERNET hSession = ::InternetOpen( s_szAgent,
PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0 ); if ( hSession )
{
HINTERNET hUrlFile = ::InternetOpenUrl(hSession,
lpszUrl,
NULL,
0,
INTERNET_FLAG_RELOAD,
0) ;
if ( hUrlFile )
{
DWORD dwOffset = 0;
DWORD dwBytesRead = 0;
// 循环读取所有数据
do
{
::InternetReadFile(hUrlFile,lpszBuffer + dwOffset,DEFAULT_BUF_SIZE,&dwBytesRead);
dwOffset += dwBytesRead; bRel = true;
}
while(dwBytesRead);
::InternetCloseHandle( hUrlFile );
}
::InternetCloseHandle( hSession );
} return bRel;
}