가장 좋은 예제는 MSDN에 있는 Tear예제이다.
/////////////////////////////////////////
int get_htmlpage(const char* filename, int nVerb,char* server,char* page,char* SessionID,char* post_payload){
CInternetSession* pInetSession;
CHttpConnection* pHttpConnection;
CHttpFile* pFile = NULL;
DWORD dwServiceType;
CString sHttpSite;
CString sServerName;
CString sObject;
CString sHttpURL;
INTERNET_PORT nPort;
int n;
char buffer[1024];
CString strHeaders;
CString strFormData=post_payload;
sHttpURL="http://";
sHttpURL+= server;
if(*page!='/')sHttpURL+="/";
sHttpURL+=page;
AfxParseURL(sHttpURL, dwServiceType, sServerName, sObject, nPort);
pInetSession = new CInternetSession(_T("HTML-Dateiubertragung"), 1, PRE_CONFIG_INTERNET_ACCESS);
pHttpConnection = pInetSession->GetHttpConnection(sServerName, nPort);
pFile = pHttpConnection->OpenRequest(nVerb,sObject, NULL, 1, NULL, NULL,INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->AddRequestHeaders("Accept: */*\r\n");
strHeaders="Host: ";
strHeaders+=server;
strHeaders+="\r\n";
strHeaders+="Accept: */* \r\n";
strHeaders+="Accept-Language: de\r\n";
strHeaders+="Pragma: no-cache\r\n";
strHeaders+="Proxy-Connection: Keep-Alive\r\n";
strHeaders+="Referer: http://www.tralala.de\r\n";
strHeaders+="User-Agent: Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)\r\n";
strHeaders+="Cookie: PHPSESSID=";
strHeaders+=SessionID;
strHeaders+="\r\n";
strHeaders+="Content-type: application/x-www-form-urlencoded\r\n";
strHeaders+="Extension: Security/Remote-Passphrase\r\n\r\n";
BOOL result = pFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
if(filename!=NULL){
CFile file;
file.Open(filename, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
while ( (n = pFile->Read( buffer, 1024 ))>0)
file.Write( buffer,n);
file.Close();
}
pFile->Close();
if (pFile != NULL)delete pFile;
if (pHttpConnection != NULL)delete pHttpConnection;
pInetSession->Close();
delete pInetSession;
return 0;
}
/////////////////////////////////////////////////////
// init sockets
AfxSocketInit();
in ljrequest...
if (proxy) {
// recieve buffer
char rcvbuf[1024];
// new socket
CSocket* pSocket = new CSocket;
pSocket->Create();
int p = 0;
p = atoi(ljr->m_settings->m_prox_port);
CString ljlog = "http://" + ljr->m_settings->m_server + ljr->m_settings->m_logurl;
if (p == 0)
p = 80;
// connect
pSocket->Connect(ljr->m_settings->m_prox_serv,INTERNET_PORT(p));
// clear buffer
::ZeroMemory(rcvbuf, sizeof(rcvbuf));
// header
CString send;
send += "POST " + ljlog + " HTTP/1.1\r\n";
send += "Content-type: application/x-www-form-urlencoded\r\n";
send += "User-Agent: LiveJournal\r\n";
send += "Host: " + ljr->m_settings->m_server + "\r\n";
send += "Cache-Control: no-cache\r\n";
send.Format("%sContent-Length: %d\r\n", send, postdata.GetLength());
if (ljr->m_settings != NULL && ljr->m_settings->cookie) {
send += "Cookie: ljfastserver=1\r\n";
}
send += "Authorization " + MIMEencode(ljr->m_settings->m_prox_user + ":" + ljr->m_settings->m_prox_pass) + "\r\n";
send += "Proxy-Authorization: Basic " + MIMEencode(ljr->m_settings->m_prox_user + ":" + ljr->m_settings->m_prox_pass) + "\r\n";
send += "\r\n";
send += postdata;
// send request
pSocket->Send(send, send.GetLength());
// receive stuff from server
int ret;
while ((ret = pSocket->Receive(rcvbuf,sizeof(rcvbuf)-1)) && ret != WSAECONNRESET) {
response += rcvbuf;
}
// delete socket
pSocket->Close();
delete pSocket;
int loc = 0;
int newloc;
while ((newloc = response.Find((TCHAR) '\n', loc)) != -1)
{
CString frag = response.Mid(loc, newloc-loc);
if (frag == "\r") {
response = response.Mid(loc+2);
break;
}
if (loc == 0) {
int start = frag.Find(' ');
CString msg;
msg.Format("frag = %s\ncode = %s", frag, frag.Mid(start+1, 3));
if ((frag.Mid(start+1, 3)).CompareNoCase("200") != 0) {
ljr->res["success"] = "FAIL";
ljr->res["errmsg"] = "Server error. Please try again shortly.";
MessageBox(NULL, msg, "server failed with these codes", MB_OK);
return 0;
}
}
loc = newloc+1;
}
}
///////////////////////////////////////////
반응형
'3.구현 > VC++' 카테고리의 다른 글
[MFC]디렉토리(폴더)내에서 파일이름들만 얻기 (0) | 2012.08.14 |
---|---|
[MFC]Resizing Dialog (0) | 2012.08.14 |
[MFC] 컨트롤 배경 투명하게 만들기 (0) | 2012.08.14 |
[MFC] 여기저기 긁어모은 코드들 (0) | 2012.08.14 |
[MFC] MainFrame/Document/VIew 프레임워크간 상호참조 (0) | 2012.08.14 |