- °³¿ä
- ¼ÒÄÏÀ» ÀÌ¿ëÇÏ´Â ¹æ¹ý
- Windows Internet API¸¦ ÀÌ¿ëÇÑ ¹æ¹ý
- ÀÎÅÍ³Ý ÀͽºÇ÷η¯¸¦ ÀÌ¿ëÇÏ´Â ¹æ¹ý
1 °³¿ä
HTTP¸¦ ÀÌ¿ëÇØ¼ ÆÄÀÏÀ» ´Ù¿î·Îµå¹Þ±â
2 ¼ÒÄÏÀ» ÀÌ¿ëÇÏ´Â ¹æ¹ý
³»ºÎÀûÀ¸·Î GET ¸í·É¾î¸¦ ÀÌ¿ëÇØ¼ ÆÄÀÏ ³»¿ëÀ» ¹Þ¾Æ¿Â´Ù.
ÆÄÀÏ »çÀÌÁÀ» ¾Ë¾Æ³»±â À§Çؼ´Â HEAD ¸í·É¾î¸¦ »ç¿ëÇÏ¸é µÈ´Ù. º¸Åë ´ÙÀ½°ú °°Àº ¸ð¾çÀÇ µ¥ÀÌÅͰ¡ ³¯¾Æ¿À´Âµ¥, ÇÊ¿äÇÑ ºÎºÐÀ» ÆÄ½ÌÇØ¼ ¾²¸é µÈ´Ù.
HTTP/1.1 200 OK
Accept-Ranges: bytes
Date: Sat, 27 Jul 2002 10:58:14 GMT
Content-Length: 776635
Content-Type: image/jpeg
Server: Apache/1.3.12 (Unix) mod_ssl/2.6.5 OpenSSL/0.9.5a
Last-Modified: Mon, 24 Jul 2000 00:13:24 GMT
ETag: "9b656-bd9bb-397b8a24"
Via: 1.1 cache-6100-edi (NetCache NetApp/5.2.1R1D12)
GetHTTP.cpp
// GetHTTP.cpp
//
// Retrieves a file using the Hyper Text Transfer Protocol
// and prints its contents to stdout.
//
// Pass the server name and full path of the file on the
// command line and redirect the output to a file. The program
// prints messages to stderr as it progresses.
//
// Example:
// GetHTTP www.idgbooks.com /index.html > index.html
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <winsock.h>
void GetHTTP(LPCSTR lpServerName, LPCSTR lpFileName);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%s: %d\n", s, WSAGetLastError())
void main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
// Check arguments
if (argc != 3)
{
fprintf(stderr,
"\nSyntax: GetHTTP ServerName FullPathName\n");
return;
}
// Initialize WinSock.dll
nRet = WSAStartup(wVersionRequested, &wsaData);
if (nRet)
{
fprintf(stderr,"\nWSAStartup(): %d\n", nRet);
WSACleanup();
return;
}
// Check WinSock version
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\nWinSock version not supported\n");
WSACleanup();
return;
}
// Set "stdout" to binary mode
// so that redirection will work
// for .gif and .jpg files
_setmode(_fileno(stdout), _O_BINARY);
// Call GetHTTP() to do all the work
GetHTTP(argv[1], argv[2]);
// Release WinSock
WSACleanup();
}
void GetHTTP(LPCSTR lpServerName, LPCSTR lpFileName)
{
// Use inet_addr() to determine if we're dealing with a name
// or an address
IN_ADDR iaHost;
LPHOSTENT lpHostEntry;
iaHost.s_addr = inet_addr(lpServerName);
if (iaHost.s_addr == INADDR_NONE)
{
// Wasn't an IP address string, assume it is a name
lpHostEntry = gethostbyname(lpServerName);
}
else
{
// It was a valid IP address string
lpHostEntry = gethostbyaddr((const char *)&iaHost,
sizeof(struct in_addr), AF_INET);
}
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
// Create a TCP/IP stream socket
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
// Find the port number for the HTTP service on TCP
LPSERVENT lpServEnt;
SOCKADDR_IN saServer;
lpServEnt = getservbyname("http", "tcp");
if (lpServEnt == NULL) saServer.sin_port = htons(80);
else saServer.sin_port = lpServEnt->s_port;
// Fill in the rest of the server address structure
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// Connect the socket
int nRet;
nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
PRINTERROR("connect()");
closesocket(Socket);
return;
}
// Format the HTTP request
char szBuffer[1024];
sprintf(szBuffer, "GET %s\n", lpFileName);
nRet = send(Socket, szBuffer, strlen(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(Socket);
return;
}
// Receive the file contents and print to stdout
while(1)
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
break;
}
fprintf(stderr,"\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == 0) break;
// Write to stdout
fwrite(szBuffer, nRet, 1, stdout);
}
closesocket(Socket);
}
3 Windows Internet API¸¦ ÀÌ¿ëÇÑ ¹æ¹ý
4 ÀÎÅÍ³Ý ÀͽºÇ÷η¯¸¦ ÀÌ¿ëÇÏ´Â ¹æ¹ý
URLDownloadToFile ÇÔ¼ö¸¦ ÀÌ¿ëÇϸé ÀÎÅÍ³Ý ÀͽºÇ÷η¯¸¦ ÅëÇØ ÆÄÀÏÀ» ´Ù¿î·Îµå¹ÞÀ» ¼ö ÀÖ´Ù. ÀÎÅÍ³Ý ÀͽºÇ÷η¯°¡ À̾î¹Þ±â/ij½Ã ó¸® µîÀ» ÀÚµ¿À¸·Î ÇØÁֱ⠶§¹®¿¡ ÆíÇÏ´Ù.
URLDownloadToFile ÇÔ¼ö¸¦ ÀÌ¿ëÇϱâ À§Çؼ´Â IBindStatusCallback ÀÎÅÍÆäÀ̽º¸¦ »ó¼Ó¹ÞÀº Ŭ·¡½º¸¦ Çϳª ¸¸µé¾î¼, OnProgress ÇÔ¼ö¸¦ ±¸ÇöÇØÁà¾ßÇÑ´Ù. OnProgress ÇÔ¼ö´Â ´Ù¿î·Îµå »óŸ¦ »ç¿ëÀÚ¿¡°Ô º¸¿©ÁÖ±â À§ÇÑ Äݹé ÇÔ¼ö´Ù. »ç¿ëÀÚ¿¡°Ô ÁøÇà »óȲÀ» º¸¿©ÁÖÁö ¾Ê¾Æµµ µÈ´Ù¸é, ºó »óÅ·ΠµÖµµ »ó°ü ¾øÁö¸¸, ±âº»ÀûÀÎ ¿¡·¯ 󸮴 Áý¾î³Ö¾î ÁÖ´Â °ÍÀÌ ÁÁ´Ù.
StatusCallBack.h
#ifndef __STATUSCALLBACK_H__
#define __STATUSCALLBACK_H__
#include <windows.h>
#include <urlmon.h>
#include <string>
//////////////////////////////////////////////////////////////////////////////
/// \class CCallback
/// \brief ÀÎÅÍ³Ý ÀͽºÇ÷η¯¸¦ ÀÌ¿ëÇØ HTTP·Î ÆÄÀÏÀ» ´Ù¿î·Îµå¹Þ±â À§ÇÑ
/// COM ÀÎÅÍÆäÀ̽º Ŭ·¡½º.
//////////////////////////////////////////////////////////////////////////////
class CCallback : public IBindStatusCallback
{
private:
std::string m_URL; ///< ´Ù¿î·Îµå¹ÞÀ» ÆÄÀÏÀÇ URL
DWORD m_Timeout; ///< ŸÀӾƿô
public:
/// \brief »ý¼ºÀÚ
CCallback(const std::string& url, DWORD timeout=INFINITE);
/// \brief ¼Ò¸êÀÚ
~CCallback();
public:
STDMETHOD(OnStartBinding)(
/* [in] */ DWORD dwReserved,
/* [in] */ IBinding __RPC_FAR *pib)
{ return E_NOTIMPL; }
STDMETHOD(GetPriority)(
/* [out] */ LONG __RPC_FAR *pnPriority)
{ return E_NOTIMPL; }
STDMETHOD(OnLowResource)(
/* [in] */ DWORD reserved)
{ return E_NOTIMPL; }
STDMETHOD(OnProgress)(
/* [in] */ ULONG ulProgress,
/* [in] */ ULONG ulProgressMax,
/* [in] */ ULONG ulStatusCode,
/* [in] */ LPCWSTR wszStatusText);
STDMETHOD(OnStopBinding)(
/* [in] */ HRESULT hresult,
/* [unique][in] */ LPCWSTR szError)
{ return E_NOTIMPL; }
STDMETHOD(GetBindInfo)(
/* [out] */ DWORD __RPC_FAR *grfBINDF,
/* [unique][out][in] */ BINDINFO __RPC_FAR *pbindinfo)
{ return E_NOTIMPL; }
STDMETHOD(OnDataAvailable)(
/* [in] */ DWORD grfBSCF,
/* [in] */ DWORD dwSize,
/* [in] */ FORMATETC __RPC_FAR *pformatetc,
/* [in] */ STGMEDIUM __RPC_FAR *pstgmed)
{ return E_NOTIMPL; }
STDMETHOD(OnObjectAvailable)(
/* [in] */ REFIID riid,
/* [iid_is][in] */ IUnknown __RPC_FAR *punk)
{ return E_NOTIMPL; }
STDMETHOD_(ULONG, AddRef)() { return 0; }
STDMETHOD_(ULONG, Release)() { return 0; }
STDMETHOD(QueryInterface)(
/* [in] */ REFIID riid,
/* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
{ return E_NOTIMPL; }
};
#endif //__BINDSTATUSCALLBACK_H__ StatusCallBack.h
#include "StatusCallback.h"
#include <shlwapi.h>
#include <mmsystem.h>
#include <iostream>
#include <assert.h>
//////////////////////////////////////////////////////////////////////////////
/// \brief »ý¼ºÀÚ
/// \param url ´Ù¿î·Îµå¹ÞÀ» ÆÄÀÏÀÇ URL
/// \param timeout ŸÀӾƿô
//////////////////////////////////////////////////////////////////////////////
CCallback::CCallback(const std::string& url, DWORD timeout)
: m_URL(url), m_Timeout(timeout)
{
assert(!m_URL.empty());
}
//////////////////////////////////////////////////////////////////////////////
/// \brief ¼Ò¸êÀÚ
//////////////////////////////////////////////////////////////////////////////
CCallback::~CCallback()
{
}
//////////////////////////////////////////////////////////////////////////////
/// \brief ÁøÇà »óȲÀ» ¾Ë·ÁÁÖ±â À§ÇØ ÀÎÅÍ³Ý ÀͽºÇ÷η¯°¡ È£ÃâÇÏ°Ô µÇ´Â
/// Äݹé ÇÔ¼ö
///
/// \param ulProgress ´Ù¿î·Îµå¹ÞÀº ¹ÙÀÌÆ® ¼ö
/// \param ulProgressMax ´Ù¿î·Îµå¹Þ¾Æ¾ßÇÒ Àüü ¹ÙÀÌÆ® ¼ö
/// \param ulStatusCode »óÅ ÄÚµå
/// \param wszStatusText »óÅ ¹®ÀÚ¿
/// \return HRESULT ÀÎÅÍ³Ý ÀͽºÇ÷η¯¿¡°Ô ¾Ë·ÁÁà¾ßÇÏ´Â °á°ú°ª. ´Ù¿î·Îµå¸¦
/// °è¼ÓÇØ¾ßÇÏ´Â °æ¿ì¿¡´Â S_OK¸¦ ¹ÝÈ¯ÇØ¾ßÇϰí, Áß´ÜÇÏ·Á¸é E_ABORT¸¦ ¹ÝÈ¯ÇØ¾ß
/// ÇÑ´Ù.
//////////////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE CCallback::OnProgress(ULONG ulProgress,
ULONG ulProgressMax,
ULONG ulStatusCode,
LPCWSTR wszStatusText)
{
// ÀÌ ÇÔ¼ö ³»ºÎ¿¡¼ Ȥ½Ã »ç¿ëÀÚ°¡ ÁßÁö ¹öưÀ» ´·¶´Ù´ø°¡,
// ŸÀӾƿôÀÌ µÇ¾ú´Ù´ø°¡, ulStatusCode°¡ ÀÌ»óÇÏ´ø°¡ Çϸé,
// E_ABORT¸¦ ¹ÝÈ¯ÇØ¼, ´Ù¿î·Îµå¸¦ Áß´ÜÇØ¾ß ÇÑ´Ù.
if (m_Timeout < timeGetTime())
{
std::cout << "´ë±â ½Ã°£ÀÌ ÃʰúµÇ¾ú½À´Ï´Ù" << std::endl;
return E_ABORT;
}
// ´Ù¿î·Îµå¸¦ ÁßÁöÇØ¾ßÇÏ´Â »óȲÀÌ ¾Æ´Ï¶ó¸é,
// ulProgress °ª°ú, ulProgressMax °ªÀ» ÀÌ¿ëÇØ
// ÇÁ·Î±×·¹½º¹Ù µîÀ» Àû´çÈ÷ ¾÷µ¥ÀÌÆ®ÇÏ¸é µÈ´Ù.
if (0 != ulProgressMax)
{
std::cout << "´Ù¿î·Îµå ÁßÀÔ´Ï´Ù - " << m_URL
<< "(" << int( 100.0 * ulProgress / ulProgressMax) ) << "%)" << std::endl;
}
else
{
std::cout << "´Ù¿î·Îµå ÁßÀÔ´Ï´Ù - " << m_URL << std::endl;
}
return S_OK;
} »ùÇÃ
string URL = "http://somewhere.com/something.zip";
string filename = "c:\something.zip";
CCallback callback(URL, timeGetTime() + 60 * 1000);
HRESULT hr = ::URLDownloadToFile(
NULL, URL.c_str(), filename.c_str(), 0, &callback);
CodeProject¿¡ ±¦ÂúÀº ¿¹Á¦°¡ ÀÖÀ¸´Ï À̸¦ Âü°íÇϸé ÁÁ´Ù.
ij½Ã¿¡ ÀÖ´Â ÆÄÀÏÀ» ¹«½ÃÇϰí, »õ·Î ¹Þ±â¸¦ ¿øÇÑ´Ù¸é DeleteUrlCacheEntry ÇÔ¼ö¸¦ ÀÌ¿ëÇÏÀÚ.
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)