edog

冰冻热狗
数据加载中……

用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 2005-12-06 00:19 冰冻热狗 阅读(22329) 评论(6)  编辑 收藏 引用 所属分类: .NET技术

评论

# re: 用CHttpFile实现简单的GET/POST数据  回复  更多评论   

我在提交后,发现服务器只能收到头一个字母.请大虾帮忙
2007-07-07 11:38 | song

# re: 用CHttpFile实现简单的GET/POST数据  回复  更多评论   

现在博客中都是垃圾,查了半天,全是拷来拷去的.
2009-03-10 14:14 | 垃圾

# re: 用CHttpFile实现简单的GET/POST数据[未登录]  回复  更多评论   

服务器只收到头一个字母,是因为你使用的Unicode模式,在编译器里将其调整为非Unicode或者将CString转为单字节模式。
2009-08-28 09:54 | 123

# re: 用CHttpFile实现简单的GET/POST数据  回复  更多评论   

你那是POST数据的代码。。。。。
2013-04-25 08:18 | 22

# re: 用CHttpFile实现简单的GET/POST数据[未登录]  回复  更多评论   

真SB
2014-08-06 13:42 | 123

# re: 用CHttpFile实现简单的GET/POST数据  回复  更多评论   

很给力啊,慢慢学习理解,消化,谢谢楼主分享。
2015-08-15 02:08 |

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理