zoukankan      html  css  js  c++  java
  • 用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+" ");
                    }
                    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();

  • 相关阅读:
    java Concurrent包学习笔记(二):CountDownLatch和CyclicBarrier
    java Concurrent包学习笔记(四):BlockingQueue
    Linux Linux程序练习十五(进程间的通信共享内存版)
    Linux shell中的符号
    Linux shell程序一
    Linux Linux程序练习十四(多进程压力测试)
    Linux Linux程序练习十三(信号阻塞,捕获)
    Linux 网络编程详解二(socket创建流程、多进程版)
    Linux 网络编程详解一(IP套接字结构体、网络字节序,地址转换函数)
    Linux shell实战(ipcs工具)
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/5624323.html
Copyright © 2011-2022 走看看