MFCを利用してHTTPアクセスをするクラス。
GETメソッドしか使えないけど、
ヘッダ情報とコンテンツぐらいは取得可能。
// Http.h: interface for the CHttp class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_HTTP_H__A1198C8D_2219_4E9E_A135_1C189078CD3D__INCLUDED_)
#define AFX_HTTP_H__A1198C8D_2219_4E9E_A135_1C189078CD3D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CHttp
{
// Attributes:
private:
CString m_strHeaders;
CString m_strContent;
// Construction/Destruction:
public:
CHttp();
virtual ~CHttp();
// Internal Opreations:
private:
void Clear();
// Opreations:
public:
void DoGet(CString strURL);
CString GetHeaders();
CString GetContent();
};
#endif // !defined(AFX_HTTP_H__A1198C8D_2219_4E9E_A135_1C189078CD3D__INCLUDED_)
>>2 のつづき。
// Http.cpp: implementation of the CHttp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxinet.h>
#include "Http.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Internal Constant
//////////////////////////////////////////////////////////////////////
static CString LINE_SEPARATOR = "\n";
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHttp::CHttp()
{
}
CHttp::~CHttp()
{
}
CString CHttp::GetContent()
{
return m_strContent;
}
CString CHttp::GetHeaders()
{
return m_strHeaders;
}
void CHttp::Clear()
{
m_strHeaders = "";
m_strContent = "";
}
>>3 のつづき。
void CHttp::DoGet(CString strURL)
{
Clear();
DWORD dwServiceType;
CString server;
CString file;
CString referer;
INTERNET_PORT nPort;
if(!AfxParseURL(strURL, dwServiceType, server, file, nPort)){
::MessageBox(NULL, "Filed to parse URL.", "Error", MB_OK);
return;
}
CInternetSession session;
CHttpConnection* pCon =
session.GetHttpConnection(
server,
CHttpConnection::HTTP_VERB_GET,
nPort,
NULL,
NULL);
CHttpFile* pFile =
pCon->OpenRequest(
// CHttpConnection::HTTP_VERB_HEAD, // for head method
CHttpConnection::HTTP_VERB_GET,
file,
NULL,
1,
NULL,
NULL,
INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
pFile->SendRequest();
// for debug
//::MessageBox(NULL, pFile->GetFileURL(), "URL", MB_OK);
// get headers
CString strHeaders;
{
DWORD dwIndex = 0;
CString buf;
while(true){
BOOL bResult =
pFile->QueryInfo(
HTTP_QUERY_RAW_HEADERS_CRLF,
buf,
&dwIndex);
if(!bResult){
break;
}
strHeaders = strHeaders + buf;
strHeaders = strHeaders + LINE_SEPARATOR;
dwIndex++;
}
}
m_strHeaders = strHeaders; // set to member variable
// get content
CString strContent;
{
CString buf;
while(pFile->ReadString(buf)){
strContent = strContent + buf;
strContent = strContent + LINE_SEPARATOR;
}
}
m_strContent = strContent; // set to member variable
pFile->Close();
session.Close();
}
>>4 の 使用例
CString strURL = "http://www.nilab.info/";
CHttp http;
http.DoGet(strURL);
CString strHeaders = http.GetHeaders();
CString strContent = http.GetContent();