一、GET 数据,下载网页,文件等,用于可下载的文件,不能用于服务端运行的程序,比如.aspx文件等,否则会返回500错误。

CString strSentence, strWriteName="1.htm";
    CString strFileName="http://localhost/InDesign/" + strWriteName;

    CInternetSession sess;
    CHttpFile* fileGet;
    try
    {
        fileGet=(CHttpFile*)sess.OpenURL(strFileName);
    }
    catch(CException* e)
    {
        fileGet = 0;
        throw;
    }    

    if(fileGet)
    {
        DWORD dwStatus;
        DWORD dwBuffLen = sizeof(dwStatus);
        BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);

        if( bSuccess && dwStatus>= 200&& dwStatus<300 ) 
        { 
            CStdioFile fileWrite; 
            if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))
            { 
                while(fileGet->ReadString(strSentence))
                {
                    fileWrite.WriteString(strSentence+"\n");
                }
                fileWrite.Close();
                AfxMessageBox("下载完毕");
            }
            else
            {
                AfxMessageBox("本地文件"+strWriteName+"打开出错."); 
            }
        }
        else 
        {
            strSentence.Format("打开网页文件出错,错误码:%d", dwStatus);
            AfxMessageBox(strSentence);
        }
        fileGet->Close();
        delete fileGet;
    }
    else
        AfxMessageBox("不能找到网页文件!");

    sess.Close();

二、POST 数据,比如用于提交注册信息等

CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交数据的页面
    CString strFormData = "username=abc&password=123";    // 需要提交的数据

    CInternetSession sess;
    CHttpFile* fileGet;
    CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头

    try
    {
        fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
    }
    catch(CException* e)
    {
        fileGet = 0;
        throw;
    }

    CString strSentence, strGetSentence = "";
    if(fileGet)
    {
        DWORD dwStatus;
        DWORD dwBuffLen = sizeof(dwStatus);
        BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
        if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
        { 
            BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
            while(fileGet->ReadString(strSentence))    // 读取提交数据后的返回结果
            {
                strGetSentence = strGetSentence + strSentence + char(13) + char(10);
            }
            AfxMessageBox(strGetSentence); // 显示返回网页内容
        }
        else 
        {
            strSentence.Format("POST出错,错误码:%d", dwStatus);
            AfxMessageBox(strSentence);
        }
        
        fileGet->Close();
        delete fileGet;
    }
    else
        AfxMessageBox("不能找到网页文件!");

    sess.Close();

最新文章

  1. 在js中为图片的src赋值时,src的值不能在开头用 破浪号~
  2. 在virtualbox中安装CentOS-7
  3. Android 常用布局视图
  4. [转]一篇很全面的freemarker教程
  5. 【LeetCode】83 - Remove Duplicates from Sorted Array
  6. HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)
  7. Android 基础知识点(持续更新)
  8. uva 12096
  9. cocoaPods使用
  10. 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现
  11. Linux之为集群内的机器设定主机名
  12. AndrodStudio报错: Cannot launch AVD in emulator.
  13. day2:day1作业 字符编码
  14. RobotFrameWork(一)robotfamework(python版)及Ride在windows安装
  15. 彻底理解MapReduce shuffle过程原理
  16. 201621123008 《Java程序设计》第14周学习总结
  17. JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
  18. iPhone/iPad被停用怎么办 3招轻松解锁
  19. LeetCode137:Single Number II
  20. Hadoop生态圈-hbase介绍-完全分布式搭建

热门文章

  1. SQLAlchemy 学习笔记(三):ORM 中的关系构建
  2. Box 类
  3. 【转】利用Behavior Designer制作敌人AI
  4. jquery版tab切换效果
  5. JS 处理json数据
  6. eclipse中 tomcat首页server Locations变灰无法编辑
  7. 如何通过IP地址分辨公网、私网、内网、外网
  8. [暑假集训--数论]poj2773 Happy 2006
  9. 分裂游戏(bzoj 1188)
  10. 17.2.10 NOIP模拟赛 藏妹子之处(excel)