Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565961
  • 博文数量: 190
  • 博客积分: 10937
  • 博客等级: 上将
  • 技术积分: 2205
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-07 11:28
文章分类

全部博文(190)

文章存档

2012年(1)

2011年(27)

2010年(20)

2009年(142)

我的朋友

分类: 系统运维

2010-04-21 17:39:22

一、GET 数据,下载网页,文件等,用于可下载的文件,不能用于服务端运行的程序,比如.aspx文件等,否则会返回500错误。

CString strSentence, strWriteName="1.htm";
    CString strFileName="" + 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=""; // 需要提交数据的页面
    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();

 

补充:  void   xx(void)  
  {  
  CHAR   szReceiveBuffer[36000];  
  memset(szReceiveBuffer,0,36000);  
  char   *req="POST   HTTP/1.0\r\n"  
  "Accept:   image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/msword,   application/vnd.ms-powerpoint,   */*\r\n"  
  "Accept-Language:   en-us\r\n"  
  "Accept-Encoding:   gzip,   deflate\r\n"  
  "User-Agent:   Mozilla/4.0\r\n"  
  "Content-Length:   34\r\n"  
  "Host:   127.0.0.1\r\n"  
  "Content-Type:   application/x-www-form-urlencoded\r\n\r\n";  
   
  LPSTR   lpOptions="user=admin&Pwd=admin&submit=提交";  
   
  CInternetSession   cInternetSession;  
  CHttpConnection   *   pHttpConnection   =   cInternetSession.GetHttpConnection(_T("127.0.0.1"),   (INTERNET_PORT)80);  
  CHttpFile   *   pHttpFile   =   pHttpConnection->OpenRequest("POST",   _T("/admin/test.asp"));  
  pHttpFile->AddRequestHeaders(req);  
  pHttpFile->SendRequest(0,0,   (LPVOID)   lpOptions,   (DWORD)strlen(lpOptions));  
  pHttpFile->Read(szReceiveBuffer,   36000);  
  cInternetSession.Close();  
  //CString   hh=szReceiveBuffer;  
  //if(hh.Find("您输入了错误的帐号或",0))  
                  MessageBox(szReceiveBuffer);  
   
  return   ;  
  }  

阅读(965) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~