博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MFC之HTTP客户端应用程序
阅读量:4508 次
发布时间:2019-06-08

本文共 5423 字,大约阅读时间需要 18 分钟。

一、建立会话(Session)对象:

       CInternetSession mysession;

二、连接到Http服务器:

      CHttpConnection *myconn=mysession.GetHttpConnection("");

三、打开Http请求:

      CHttpFile *myfile=myconn->OpenRequest("GET","/index.html");

四、发送Http请求:

      myfile->SendRequest();

五、从服务器读取字节流(bytes):

      CString mystr;

      CString tmp;
      while(myfile->ReadString(tmp))
      {
           mystr+=tmp;
      }

六、释放资源:

     myfile->Close();

      myconn->Close();
     mysession.Close();
     delete myfile;
     delete myconn;
     myfile=0;
     myconn=0;

     步骤基本如上,上例各函数参数主要使用默认参数,具体函数声明请查阅MSDN。另外别忘了异常处理!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1、下载文件

Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
 ASSERT(strFileURLInServer != "");
 ASSERT(strFileLocalFullPath != "");
 CInternetSession session;
 CHttpConnection* pHttpConnection = NULL;
 CHttpFile* pHttpFile = NULL;
 CString strServer, strObject;
 INTERNET_PORT wPort;

 

 DWORD dwType;

 const int nTimeOut = 2000;
 session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
 session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);   //重试次数
 char* pszBuffer = NULL;  

 

 try

 {
  AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
  pHttpConnection = session.GetHttpConnection(strServer, wPort);
  pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
  if(pHttpFile->SendRequest() == FALSE)
   return false;
  DWORD dwStateCode;

 

  pHttpFile->QueryInfoStatusCode(dwStateCode);

  if(dwStateCode == HTTP_STATUS_OK)
  {
    HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
         FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
         NULL);  //创建本地文件
   if(hFile == INVALID_HANDLE_VALUE)
   {
    pHttpFile->Close();
    pHttpConnection->Close();
    session.Close();
    return false;
   }
 
   char szInfoBuffer[1000];  //返回消息
   DWORD dwFileSize = 0;   //文件长度
   DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
   BOOL bResult = FALSE;
   bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
           (void*)szInfoBuffer,&dwInfoBufferSize,NULL);

 

   dwFileSize = atoi(szInfoBuffer);

   const int BUFFER_LENGTH = 1024 * 10;
   pszBuffer = new char[BUFFER_LENGTH];  //读取文件的缓冲
   DWORD dwWrite, dwTotalWrite;
   dwWrite = dwTotalWrite = 0;
   UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据

 

   while(nRead > 0)

   {
    WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);  //写到本地文件
    dwTotalWrite += dwWrite;
    nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
   }

 

   delete[]pszBuffer;

   pszBuffer = NULL;
   CloseHandle(hFile);
  }
  else
  {
   delete[]pszBuffer;
   pszBuffer = NULL;
   if(pHttpFile != NULL)
   {
    pHttpFile->Close();
    delete pHttpFile;
    pHttpFile = NULL;
   }
   if(pHttpConnection != NULL)
   {
    pHttpConnection->Close();
    delete pHttpConnection;
    pHttpConnection = NULL;
   }
   session.Close();
    return false;
  }
 }
 catch(...)
 {
  delete[]pszBuffer;
  pszBuffer = NULL;
  if(pHttpFile != NULL)
  {
   pHttpFile->Close();
   delete pHttpFile;
   pHttpFile = NULL;
  }
  if(pHttpConnection != NULL)
  {
   pHttpConnection->Close();
   delete pHttpConnection;
   pHttpConnection = NULL;
  }
  session.Close();
  return false;
 }

 

 if(pHttpFile != NULL)

  pHttpFile->Close();
 if(pHttpConnection != NULL)
  pHttpConnection->Close();
 session.Close();
 return true;
}
2、上传文件
bool Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
ASSERT(strFileURLInServer != "");
ASSERT(strFileLocalFullPath != "");
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
bool bReturn = false;
DWORD dwType;
const int nTimeOut = 2000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重试次数
char* pszBuffer = NULL;
try
{
AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
pHttpConnection = session.GetHttpConnection(strServer, wPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
if(pHttpFile->SendRequest() == FALSE)
return false;
DWORD dwStateCode;
pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
{
HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
pHttpConnection->Close();
session.Close();
return false;
}
char szInfoBuffer[1000]; //返回消息
DWORD dwFileSize = 0; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL);
dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = 0;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据
while(nRead > 0)
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
dwTotalWrite += dwWrite;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
}
delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
bReturn = true;
}
}
catch(CInternetException* e)
{
TCHAR tszErrString[256];
e->GetErrorMessage(tszErrString, ArraySize(tszErrString));
TRACE(_T("Download XSL error! URL: %s,Error: %s"), strFileURLInServer, tszErrString);
e->Delete();
}
catch(...)
{
}
if(pszBuffer != NULL)
{
delete[]pszBuffer;
}
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
}
session.Close();
return bReturn;

 

 

转载于:https://www.cnblogs.com/Ray-chen/archive/2011/12/14/2287812.html

你可能感兴趣的文章
mysql 行转列 和 列转行
查看>>
[Leetcode]
查看>>
再谈vertical-align与line-height
查看>>
有关时延扩展的双语句子
查看>>
工作多年后积累的设计灵活,稳定,优秀WinForms应用程序的最佳实践 WinForms best practice...
查看>>
iOS开发——高级篇——iOS键盘的相关设置(UITextfield)
查看>>
JVMGC机制
查看>>
IAR for AVR 报array is too large错误 【已解决】
查看>>
老子《道德经》第六十二章
查看>>
Junit问题01 利用 @Autowired 注入失效问题
查看>>
20180711
查看>>
Js常见的创建对象
查看>>
IOS拖动
查看>>
httpclient的使用
查看>>
Kafka集群副本分配算法解析
查看>>
vue单页面条件下添加类似浏览器的标签页切换功能
查看>>
lambda表达式10个示例——学习笔记
查看>>
python 文件操作
查看>>
Java多线程之后台线程
查看>>
浏览器兼容性
查看>>