• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            edog

            冰凍熱狗
            數(shù)據(jù)加載中……

            用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)

            一、GET 數(shù)據(jù),下載網(wǎng)頁,文件等,用于可下載的文件,不能用于服務端運行的程序,比如.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(
            "打開網(wǎng)頁文件出錯,錯誤碼:%d", dwStatus);
                        AfxMessageBox(strSentence);
                    }

                    fileGet
            ->Close();
                    delete fileGet;
                }

                
            else
                    AfxMessageBox(
            "不能找到網(wǎng)頁文件!");

                sess.Close();

            二、POST 數(shù)據(jù),比如用于提交注冊信息等
            CString strHttpName="http://localhost/TestReg/RegForm.aspx"// 需要提交數(shù)據(jù)的頁面
                CString strFormData = "username=abc&password=123";    // 需要提交的數(shù)據(jù)

                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))    // 讀取提交數(shù)據(jù)后的返回結果
                        {
                            strGetSentence 
            = strGetSentence + strSentence + char(13+ char(10);
                        }

                        AfxMessageBox(strGetSentence); 
            // 顯示返回網(wǎng)頁內容
                    }

                    
            else 
                    
            {
                        strSentence.Format(
            "POST出錯,錯誤碼:%d", dwStatus);
                        AfxMessageBox(strSentence);
                    }

                    
                    fileGet
            ->Close();
                    delete fileGet;
                }

                
            else
                    AfxMessageBox(
            "不能找到網(wǎng)頁文件!");

                sess.Close();

            posted on 2005-12-06 00:19 冰凍熱狗 閱讀(22566) 評論(6)  編輯 收藏 引用 所屬分類: .NET技術

            評論

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)  回復  更多評論   

            我在提交后,發(fā)現(xiàn)服務器只能收到頭一個字母.請大蝦幫忙
            2007-07-07 11:38 | song

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)  回復  更多評論   

            現(xiàn)在博客中都是垃圾,查了半天,全是拷來拷去的.
            2009-03-10 14:14 | 垃圾

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)[未登錄]  回復  更多評論   

            服務器只收到頭一個字母,是因為你使用的Unicode模式,在編譯器里將其調整為非Unicode或者將CString轉為單字節(jié)模式。
            2009-08-28 09:54 | 123

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)  回復  更多評論   

            你那是POST數(shù)據(jù)的代碼。。。。。
            2013-04-25 08:18 | 22

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)[未登錄]  回復  更多評論   

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

            # re: 用CHttpFile實現(xiàn)簡單的GET/POST數(shù)據(jù)  回復  更多評論   

            很給力啊,慢慢學習理解,消化,謝謝樓主分享。
            2015-08-15 02:08 |
            热99RE久久精品这里都是精品免费 | 综合久久精品色| 久久中文字幕精品| 精品免费久久久久久久| 久久se这里只有精品| 亚洲国产另类久久久精品小说 | 97精品伊人久久久大香线蕉 | 久久久精品波多野结衣| 久久久久久久波多野结衣高潮| 久久亚洲精品中文字幕| 久久久99精品成人片中文字幕| 亚洲欧洲日产国码无码久久99| 久久97久久97精品免视看| 性高湖久久久久久久久| 国产精品成人无码久久久久久 | yy6080久久| 久久精品国产亚洲5555| 97久久精品午夜一区二区| 久久综合亚洲鲁鲁五月天| 久久国产一片免费观看| 久久福利青草精品资源站| 亚洲香蕉网久久综合影视| 亚洲性久久久影院| 精品久久久久久无码国产| 久久精品国产福利国产秒| 国内精品久久久久久99| 99久久综合国产精品免费| 婷婷久久精品国产| 亚洲精品97久久中文字幕无码| 中文字幕亚洲综合久久| 中文字幕一区二区三区久久网站| 999久久久无码国产精品| 久久精品aⅴ无码中文字字幕不卡| 国产69精品久久久久观看软件 | 一级做a爱片久久毛片| 亚洲国产精品婷婷久久| 精品人妻久久久久久888| 久久99国产精品二区不卡| 国产精品久久成人影院| 久久久久一区二区三区| 久久国产午夜精品一区二区三区|