随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

用CHttpFile实现简单的GET/POST数据【转】

一、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();

posted on   3D入魔  阅读(1012)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2007-06-28 64位进程调用32位dll的解决方法
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示