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

本文介绍了如何使用C++通过GET方式下载网页或文件,并通过POST方式提交注册信息等数据。提供了具体的代码实现步骤,包括打开URL、发送请求及处理响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、GET 数据,下载网页,文件等,用于可下载的文件,不能用于服务端运行的程序,比如.aspx文件等,否则会返回500错误。
 1CString strSentence, strWriteName="1.htm";
 2    CString strFileName="http://localhost/InDesign/" + strWriteName;
 3
 4    CInternetSession sess;
 5    CHttpFile* fileGet;
 6    try
 7    {
 8        fileGet=(CHttpFile*)sess.OpenURL(strFileName);
 9    }

10    catch(CException* e)
11    {
12        fileGet = 0;
13        throw;
14    }
   
15
16    if(fileGet)
17    {
18        DWORD dwStatus;
19        DWORD dwBuffLen = sizeof(dwStatus);
20        BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
21
22        if( bSuccess && dwStatus>= 200&& dwStatus<300 )
23        {
24            CStdioFile fileWrite;
25            if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))
26            {
27                while(fileGet->ReadString(strSentence))
28                {
29                    fileWrite.WriteString(strSentence+"\n");
30                }

31                fileWrite.Close();
32                AfxMessageBox("下载完毕");
33            }

34            else
35            {
36                AfxMessageBox("本地文件"+strWriteName+"打开出错.");
37            }

38        }

39        else
40        {
41            strSentence.Format("打开网页文件出错,错误码:%d", dwStatus);
42            AfxMessageBox(strSentence);
43        }

44        fileGet->Close();
45        delete fileGet;
46    }

47    else
48        AfxMessageBox("不能找到网页文件!");
49
50    sess.Close();

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

 1 CString strHttpName="http://localhost/TestReg/RegForm.aspx"// 需要提交数据的页面
 2    CString strFormData = "username=abc&password=123";    // 需要提交的数据
 3
 4    CInternetSession sess;
 5    CHttpFile* fileGet;
 6    CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头
 7
 8    try
 9    {
10        fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
11    }

12    catch(CException* e)
13    {
14        fileGet = 0;
15        throw;
16    }

17
18    CString strSentence, strGetSentence = "";
19    if(fileGet)
20    {
21        DWORD dwStatus;
22        DWORD dwBuffLen = sizeof(dwStatus);
23        BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
24        if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
25        {
26            BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
27            while(fileGet->ReadString(strSentence))    // 读取提交数据后的返回结果
28            {
29                strGetSentence = strGetSentence + strSentence + char(13+ char(10);
30            }

31            AfxMessageBox(strGetSentence); // 显示返回网页内容
32        }

33        else
34        {
35            strSentence.Format("POST出错,错误码:%d", dwStatus);
36            AfxMessageBox(strSentence);
37        }

38       
39        fileGet->Close();
40        delete fileGet;
41    }

42    else
43        AfxMessageBox("不能找到网页文件!");
44
45    sess.Close();
46

转载于:https://www.cnblogs.com/kaixuan/archive/2008/01/31/1060233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值