//---------------------------------------------------------------------------||
//-- Post_Query
//---------------------------------------------------------------------------||
char * Post_Query (char * szURL, char * szPostData)
{
CAString aString;
aString = "";
HINTERNET hSession = ::InternetOpen ("TestOpen", 0, NULL, NULL, NULL);
if (hSession)
{
// crack url..
char szExtraInfo [MAX_PATH];
char szHostName [MAX_PATH];
char szPassword [MAX_PATH];
char szScheme [MAX_PATH];
char szUrlPath [MAX_PATH * 4];
char szUserName [MAX_PATH];
URL_COMPONENTS urlcomponent;
urlcomponent.dwExtraInfoLength = MAX_PATH;
urlcomponent.dwHostNameLength = MAX_PATH;
urlcomponent.dwPasswordLength = MAX_PATH;
urlcomponent.dwSchemeLength = MAX_PATH;
urlcomponent.dwStructSize = sizeof(URL_COMPONENTS);
urlcomponent.dwUrlPathLength = MAX_PATH * 4;
urlcomponent.dwUserNameLength = MAX_PATH;
urlcomponent.lpszExtraInfo = szExtraInfo;
urlcomponent.lpszHostName = szHostName;
urlcomponent.lpszPassword = szPassword;
urlcomponent.lpszScheme = szScheme;
urlcomponent.lpszUrlPath = szUrlPath;
urlcomponent.lpszUserName = szUserName;
urlcomponent.nPort = 0;
//urlcomponent.nScheme = 0;
::InternetCrackUrl(szURL, sizeof(URL_COMPONENTS), NULL, &urlcomponent);
// download user
HINTERNET hConnect = ::InternetConnect (hSession,
urlcomponent.lpszHostName,
urlcomponent.nPort,
urlcomponent.lpszUserName,
urlcomponent.lpszPassword,
urlcomponent.nScheme,
NULL,
NULL);
if (hConnect)
{
// connected server..
HINTERNET hObject = ::HttpOpenRequest ( hConnect,
szPostData == NULL ? "GET" : "POST",
urlcomponent.lpszUrlPath,
"HTTP/1.1",
"text/*" ,
NULL,
INTERNET_FLAG_RELOAD,
NULL);
if (hObject)
{
// post header
char szLen[MAX_PATH];
CAString aHeader;
aHeader = "";
wsprintf(szLen, "%d", szPostData == NULL ? 0 : strlen(szPostData));
aHeader +=_T("Accept: text/*\r\n");
aHeader +=_T("User-Agent: Mozilla/4.0 (compatible; MSIE 5.0;* Windows NT)\r\n");
aHeader +=_T("Content-type: application/x-www-form-urlencoded\r\n");
aHeader +=_T("Content-length: ");
aHeader += szLen;
aHeader +=_T("\r\n\n");
::HttpAddRequestHeaders(hObject,(LPCTSTR)aHeader.m_pString,-1L,HTTP_ADDREQ_FLAG_ADD);
// post data..
if (szPostData)
{
::HttpSendRequest(hObject,NULL,0L,(LPVOID)szPostData,strlen(szPostData));
}
else
{
::HttpSendRequest(hObject,NULL,0L,NULL,0);
}
// open object
char szBuffer[1024];
DWORD dwRead = 0;
//DWORD dwWrite = 0;
while (::InternetReadFile(hObject, szBuffer, 1023, &dwRead) &&
dwRead > 0)
{
szBuffer[dwRead] = 0;
aString += szBuffer;
Sleep(1);
}
::InternetCloseHandle(hObject);
//bDownLoad = TRUE;
}
}
}
if (aString.m_pString && aString.GetLength() > 0)
{
char * pReturn = new char[aString.GetLength() + 1];
strcpy(pReturn, aString.m_pString);
return pReturn;
}
return 0;
}
저는 이렇게 만들어서 썼었는데, 대충 잘 동작하는 것 같습니다.
요구사항에 맞게 수정해서 쓰셔도 될 듯 합니다.
CAString aString;
과 같은 것들은 자체 라이브러리이니.. CString 이나 char * 등으로 맞게 변환해서 쓰시면 됩니다.
|