📄 internetsession.cpp
字号:
// InternetSession.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "InternetSession.h"
#include "afxinet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("致命错误:MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
CInternetSession* pInetSesn = new CInternetSession();
try
{
// GetFtpConnection Function
/*
CFtpConnection* pConn = NULL;
CInternetFile* pCmdSuccess = NULL;
pConn = pInetSesn->GetFtpConnection( _T("202.38.242.29"),NULL, NULL, 7626 );
if( pConn != NULL )
{
CFtpFileFind ftpf(pConn);
BOOL bSucc = ftpf.FindFile("*");
while( bSucc )
{
bSucc = ftpf.FindNextFile();
cout << (LPCTSTR)ftpf.GetFileName() << endl;
}
}
cout << "\ncommand successed." <<endl;
*/
// OpenURL 函数
/*
CStdioFile* pFile = pInetSesn->OpenURL("http://202.38.242.29/HelloWorld.htm");
CString buf;
pFile->ReadString(buf);
cout << (LPCTSTR)buf << endl;
*/
/*
HINTERNET hInet = NULL;
switch( pInetSesn->ServiceTypeFromHandle(pInetSesn->m_hSession) )
{
case INTERNET_SERVICE_FTP:
cout << "Ftp service." << endl;
break;
case INTERNET_SERVICE_HTTP:
cout << "Http service." << endl;
break;
case INTERNET_SERVICE_GOPHER:
cout << "Goeher service." << endl;
break;
case AFX_INET_SERVICE_FILE:
cout << "File service." << endl;
break;
default:
break;
}
*/
// SetCookie
/*
if( pInetSesn->SetCookie("http://202.38.242.29/", "SetCookieExample", "HelloWorld"))
cout << "Succeed." << endl;
*/
CHttpConnection* pConn = NULL;
pConn = pInetSesn->GetHttpConnection("202.38.242.29", (INTERNET_PORT)4001);
CHttpFile* pFile = pConn->OpenRequest(CHttpConnection::HTTP_VERB_GET,
"Hello.zip", NULL, 1, NULL, NULL,
INTERNET_FLAG_EXISTING_CONNECT |
INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->AddRequestHeaders("Accept:*/*\r\nAccept-Language: zh-cn\r\nAccept-Encoding: gzip, deflate\r\nConnection: Keep -Alive");
pFile->SendRequest();
cout << "Succeed." << endl;
}
catch(CInternetException* pEx)
{
pEx->ReportError();
pEx->Delete();
}
if (pInetSesn!= NULL)
pInetSesn ->Close();
}
getchar();
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -