读取HTML返回值的一个方法

本文详细介绍了使用HTTP协议进行数据上传至服务器的过程,包括建立连接、发送请求、接收响应等关键步骤,并通过示例代码展示了具体实现方法。

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

//调用方法
BOOL bRecv = HttpSend(TEXT("255,255,255,255"),TEXT("导航"),TEXT("内容"),TRUE);


void DisplayStat(LPSTR lBuffer)
{
 OutputDebugStringA(lBuffer); 
 OutputDebugStringA("\n"); 
}

/////////////////////////////////以下为函数源码
BOOL HttpSend(CString strSever, CString Object,CString strValue,BOOL bIsAuto)
{
 CHttpConnection* m_Connection = NULL;
 CHttpFile* m_File = NULL;
 CString m_Ret;//用来接收返回信息
 CInternetSession m_Session(AfxGetAppName (),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
 TRY
 {
    m_Session.SetOption (INTERNET_OPTION_CONNECT_RETRIES,1);
    m_Session.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT,20000);
    m_Session.SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT,20000);
    INTERNET_PORT nPort = 80;
    CString strServer = strSever;
    m_Connection = m_Session.GetHttpConnection(strServer,nPort,NULL,NULL );
    if (m_Connection == NULL)
    {
     DisplayStat ("An error occurred connecting to the server");
     Sleep (2000);
     goto end;
    }
    LPCTSTR ppszAcceptTypes[2];
    ppszAcceptTypes[0] = _T("*/*");
    ppszAcceptTypes[1] = NULL;
    CString strObject = Object;
    strObject = Object + strValue;
    m_File = m_Connection->OpenRequest(NULL, strObject, NULL,1,ppszAcceptTypes, NULL, INTERNET_FLAG_RELOAD |
          INTERNET_FLAG_DONT_CACHE);
    if (m_File == NULL)
    {
     DisplayStat ("打开服务器上的文件出错");
    goto end;
    }
    if (!m_File->SendRequest (NULL,0,NULL,0))
    {
     DisplayStat ("An error occurred connecting to the server");
     Sleep (2000);
     goto end;
    }
    TCHAR szStatusCode[32];
    DWORD dwInfoSize = 32;
    if (m_File->QueryInfo (HTTP_QUERY_STATUS_CODE,szStatusCode,&dwInfoSize))
    {
     long nStatusCode = _ttol(szStatusCode);
     if (nStatusCode == HTTP_STATUS_REQUEST_TIMEOUT)
     {
      DisplayStat ("连接超时,请重试");
      goto end;
     }
       else if (nStatusCode != HTTP_STATUS_OK)
     {
     DisplayStat("服务器端错误");
     goto end;
     }
     //状态正确,开始读取信息
     }
     else
     {
     DisplayStat("无法取得服务器返回信息");
     goto end;
     }

    char szReadBuf[1024];
    DWORD dwBytesToRead = 1024;
    DWORD m_ActualSize = 0;
    do
    {
    ZeroMemory (szReadBuf,1024);
     DWORD m_ActualSize = m_File->Read (szReadBuf,dwBytesToRead);
    m_Ret = m_Ret + CString(szReadBuf);
    }while(!(m_ActualSize < dwBytesToRead));

    TRACE("%s",m_Ret);
    int iFirst = m_Ret.Find (TEXT("requert:"),0);
    CString strTemp = m_Ret.Left(iFirst+sizeof(TEXT("requert:"))/2+1);
    int iLast = strTemp.Find (TEXT("requert:"),0);
    strTemp = strTemp.Left (iLast+sizeof(TEXT("requert:"))/2+1);
    if (strTemp == "requert:1")   //操作成功
    {
     DisplayStat("数据上传成功");
     if (m_File)
     {
    m_File->Close ();
      delete m_File;
     }
     if (m_Connection)
     {
     m_Connection->Close ();
      delete m_Connection;
     }
     m_Session.Close ();
     DisplayStat ("空闲。。。");
     return TRUE;
    }
    else if(strTemp == "requert:0")  //操作失败
    {
     DisplayStat("操作失败");
     if (m_File)
     {
      m_File->Close ();
      delete m_File;
     }
     if (m_Connection)
     {
      m_Connection->Close ();
      delete m_Connection;
     }
     m_Session.Close ();
     DisplayStat ("空闲。。。");
     return FALSE;
    }
    else
    {
     DisplayStat("数据上传失败,请稍候重试");
     goto end;
    }
  }
  CATCH (CInternetException, e)
  {
   TCHAR errStr[100];
   e->GetErrorMessage (errStr,100);
   OutputDebugString(errStr);
   OutputDebugString(TEXT("\n"));
   if (m_File)
   {
    m_File->Close ();
    delete m_File;
    m_File = NULL;
   }
   if (m_Connection)
   {
    m_Connection->Close ();
    delete m_Connection;
    m_Connection = NULL;
   }
    m_Session.Close ();
    DisplayStat ("空闲。。。");
    return FALSE;
   }
   END_CATCH
   end:
   if (m_File)
   {
    m_File->Close ();
    delete m_File;
    m_File = NULL;
   }
   if (m_Connection)
   {
    m_Connection->Close ();
    delete m_Connection;
    m_Connection = NULL;
   }
   m_Session.Close ();
   DisplayStat ("空闲。。。");
 return FALSE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值