用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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值