⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadbmp.c

📁 brew 用IWEB接口来实现下载图片的一个列子
💻 C
📖 第 1 页 / 共 3 页
字号:
#endif 

      // test nowaitconn, this only comes into effect if you build webber 
      // with multiple WebActions (see the definition of struct Webber)
      awo[i].nId  = WEBOPT_FLAGS;
      awo[i].pVal = (void *)WEBREQUEST_NOWAITCONN;
      i++;
      

      // test forcenew, uncomment this section to try multiple simultaneous
      // "forced" new connections. Forced new connections are not kept alive
      // unless they are the first forced new connection to a host
#ifdef TEST_FORCENEWCONN
      awo[i].nId  = WEBOPT_FLAGS;
      awo[i].pVal = (void *)WEBREQUEST_FORCENEWCONN;
      i++;
#endif 


      // turn off HTTP over HTTP proxying
      awo[i].nId  = WEBOPT_PROXYSPEC;
      awo[i].pVal = (void *)"http:///";
      i++;

      // turn on ALL proxying.  Proxyspecs are examined in WebOpt
      // order, so in this list, with the above and below PROXYSPECs,
      // everything except HTTP will be proxied through
      // http://webproxy.yourdomain.com:8080, (which you'll have to
      // set up to test, sorry
      awo[i].nId  = WEBOPT_PROXYSPEC;
      awo[i].pVal = (void *)"*:///http://192.168.1.46:80";
      i++;

      // Marks the end of the array of WebOpts
      awo[i].nId  = WEBOPT_END;
      
      // Add Options
      IWEB_AddOpt(pApp->m_pIWeb,awo);
   }

   // Initialize all my WebActions to point to  applet
    FOR_ALL_WEBACTIONS(pApp, p, p->pParent = pApp);
	FREE(pDeviceinfo);
	//FREE(pImage);
	return TRUE;
}
static boolean DB_CreateMainControls( CDBApp* pApp )
{
   AEERect rc;
   AEEItemStyle rNormalStyle;
   AEEItemStyle rSelStyle;

   // Create the main application controls.
   //生成
   if((ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_MENUCTL, (void**)(&pApp->m_pMenu)) != SUCCESS) ||
	   (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_WEB, (void **)(&pApp->m_pIWeb)) != SUCCESS)||
      /*(ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_SOFTKEYCTL, (void**)(&pApp->m_pSK)) != SUCCESS) ||*/
      (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_NET, (void**)(&pApp->m_pINetMgr)) != SUCCESS) ||
	  (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_HTML, (void**)(&pApp->m_pHTMLViewer)) != SUCCESS) ||
	  (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_FILEMGR, (void**)(&pApp->m_pFileMgr)) != SUCCESS)/* ||
      (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_TEXTCTL, (void**)(&pApp->m_pArea)) != SUCCESS ) ||
      (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_TEXTCTL, (void**)(&pApp->m_pText)) != SUCCESS )*/)
      return FALSE;

  // Let's reset text control size so on subsequent this won't misbehave by erasing screen
  SETAEERECT(&rc, 0, 0, 0, 0);
  /*ITEXTCTL_SetRect(pApp->m_pText, &rc);
  ITEXTCTL_SetRect(pApp->m_pArea, &rc);*/
  IHTMLVIEWER_SetNotifyFn(pApp->m_pHTMLViewer, (PFNHVIEWNOTIFY)DB_NotifyCB, pApp);
  IHTMLVIEWER_SetProperties(pApp->m_pHTMLViewer, HVP_SCROLLBAR);

  // Set the style of the menu control so that all the icons are displayed as
  // transparent.  This is set based on the default menu styles.
  //设置菜单样式
  ISHELL_GetItemStyle( pApp->a.m_pIShell, AEE_IT_MENU, &rNormalStyle, &rSelStyle );
  rNormalStyle.roImage = AEE_RO_TRANSPARENT;
  rSelStyle.roImage = AEE_RO_TRANSPARENT;
  IMENUCTL_SetStyle( pApp->m_pMenu, &rNormalStyle, &rSelStyle );

  return TRUE;
}
static void WebAction_Start(WebAction *pwa, char *pszUrl)
{
   ISourceUtil *pisu;
   CDBApp * pApp = pwa->pParent;
   char psz[300]="http://218.104.189.21/11.bmp ";
  //pszUrl=psz;
   // look to see if there's POST data, this is totally non-standard, but 
   // easy to put into tests
   pwa->pszPostData = STRCHR(pszUrl, 1);
   
   // if there's post data, construct a stream for IWeb to consume
   if ((char *)0 != pwa->pszPostData) {
      *pwa->pszPostData = 0;
      if (SUCCESS == 
          ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_SOURCEUTIL, 
                                (void **)&pisu)) {
         
         ISOURCEUTIL_PeekFromMemory(pisu, pwa->pszPostData + 1, 
                                    STRLEN(pwa->pszPostData + 1), 0, 0,
                                    &pwa->pipPostData);
         ISOURCEUTIL_Release(pisu);
      }
   }

   // initialize the callback, where I'll be called when the request
   // completes
   //初始化回掉函数
   CALLBACK_Init(&pwa->cb, WebAction_GotResp, pwa);
   //INETMGR_GetHostByName(pApp->m_pINetMgr,&pApp->m_dnsr, pszUrl,&pwa->cb);
   
   pwa->uStart = GETUPTIMEMS();
   // start transaction, pass callbacks for web status, web headers
   // the extra WEBOPT_HEADER is used to help test what's sent 
   // (snoop.sh above shows all the headers)
   //向服务器UPLOAD FILE
   if ((IPeek *)0 != pwa->pipPostData) {
      IWEB_GetResponse(pApp->m_pIWeb,
                       (pApp->m_pIWeb, &pwa->piWResp, &pwa->cb, pszUrl, 
                        WEBOPT_HANDLERDATA, pwa,
                        WEBOPT_HEADER, "X-Method: POST\r\n", /* for kicks */
                        WEBOPT_HEADERHANDLER, WebAction_Header, 
                        WEBOPT_STATUSHANDLER, WebAction_Status, 
                        WEBOPT_METHOD, "POST",
                        WEBOPT_BODY, pwa->pipPostData,
                        WEBOPT_CONTENTLENGTH, STRLEN(pwa->pszPostData + 1),
                        WEBOPT_END));
   } else {
	  //从服务器下载文件
     /* IWEB_GetResponse(pApp->m_pIWeb,
                       (pApp->m_pIWeb, &pwa->piWResp, &pwa->cb, pszUrl, 
                        WEBOPT_HANDLERDATA, pwa, 
                        WEBOPT_HEADER, "X-Method: GET\r\n",
                        WEBOPT_HEADERHANDLER, WebAction_Header, 
                        WEBOPT_STATUSHANDLER, WebAction_Status, 
                        WEBOPT_END));*/
	   IWEB_GetResponse(pApp->m_pIWeb,
		   (pApp->m_pIWeb, &pwa->piWResp, &pwa->cb,  pszUrl, 
                        WEBOPT_HANDLERDATA, pwa, 
                        WEBOPT_HEADER, "X-Method: GET\r\n",
						WEBOPT_PROXYSPEC, "http:///",
                        WEBOPT_PROXYSPEC, "*:///http://192.168.1.46:80",

                        WEBOPT_HEADERHANDLER, WebAction_Header, 
                        WEBOPT_STATUSHANDLER, WebAction_Status, 
                        WEBOPT_END));
   }
}

/*===========================================================================

FUNCTION: WebAction_GotResp

DESCRIPTION:
   Web transaction callback, fires when the response body is ready to
   be read, or when an error has occurred

PARAMETERS:
   p: the WebAction

DEPENDENCIES:
   None

RETURN VALUE:
   None

SIDE EFFECTS:
   None
===========================================================================*/
static void WebAction_GotResp(void *p)
{
   WebAction *pwa = (WebAction *)p;
   CDBApp * pApp = pwa->pParent; 
   WebRespInfo *pwri;

   // get information about how the web transaction went
   // pwa->piWResp is ***NEVER NULL***, even though the transaction may fail
   // for wont of memory
   pwri = IWEBRESP_GetInfo(pwa->piWResp);

   //ND_Print(pApp, "** got response...\n** info code: %d\n", pwri->nCode);
   //ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_CANCEL,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));

   // body may be NULL
   if(pwri->nCode==-522)
   {
	   ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_DNS,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
	   StartProgressDisplay(pApp);
	   ISHELL_CancelTimer(pApp->a.m_pIShell,(PFNNOTIFY)(StartProgressDisplay),pApp);
       IIMAGE_Stop(pApp->m_pProgressImages[PROG_ANIM]);
   }
   if ((ISource *)0 != pwri->pisMessage) {
      ISourceUtil *pisu;

      // read the body by creating an IGetLine to help parse into lines
         
      // This step is not actually necessary if you don't need line-by-line
      // output.  In that case, use pisMessage directly.
      ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_SOURCEUTIL,
                            (void **)&pisu);
      
      if ((ISourceUtil *)0 != pisu) {
         ISOURCEUTIL_GetLineFromSource(pisu, pwri->pisMessage, 1100, 
                                       &pwa->piGetLine);
         ISOURCEUTIL_Release(pisu);
      }
   }
   
   if ((IGetLine *)0 != pwa->piGetLine) {

      pwa->uRecvStart = GETUPTIMEMS(); // record that I started reading
      
      // got an IGetLine, go ahead and read it
      pwa->nBytes = 0;
      WebAction_ReadLines(pwa);

   } else {
      // ug, tranaction failed or couldn't get an IGetLine, oh well, bail
      //ND_Print(pApp, "** no response\n");
	   ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_NO_RESPONS,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
        IIMAGE_Stop(pApp->m_pProgressImages[PROG_ANIM]);
		ISHELL_CancelTimer(pApp->a.m_pIShell,(PFNNOTIFY)(StartProgressDisplay),pApp);
      WebAction_Stop(pwa); // stop cleans up
   }
   
}

/*===========================================================================

FUNCTION: WebAction_Header

DESCRIPTION:
   Received header callback for a web transaction. cpszName is NULL in the case 
   of continuation header line parts.

PARAMETERS:
   p: a WebAction (the subscriber)
   cpszName: the name of the web header (like "Content-Type")
   pglVal: the value of the header, like "text/html"

DEPENDENCIES:
   None

RETURN VALUE:
   None

SIDE EFFECTS:
   None
===========================================================================*/
//返回信息头
static void WebAction_Header(void *p, const char *cpszName, GetLine *pglVal)
{
   WebAction *pwa = (WebAction *)p;
   CDBApp * pApp = pwa->pParent;
  
    if ((char *)0 != cpszName) {
    // ND_Print(pApp, "%s:", cpszName);
		;
    }
    //ND_Print(pApp, "%s\n", pglVal->psz);
}

/*===========================================================================

FUNCTION: WebAction_Status

DESCRIPTION:
   Web status callback for a Web transaction 

PARAMETERS:
   p: a WebAction (the subscriber)
   ws: type of status
   pVal: unused as of yet

DEPENDENCIES:
   None

RETURN VALUE:
   None

SIDE EFFECTS:
   None
===========================================================================*/
//IWeb 状态
static void WebAction_Status(void *p, WebStatus ws, void *pVal)
{
   char *      pszStatus = 0;
   WebAction * pwa = (WebAction *)p;
   CDBApp * pApp = pwa->pParent;
   
   (void)pVal;

   switch (ws) {
   case WEBS_CANCELLED:
	   ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_CANCEL,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
      pszStatus = "** cancelled...\n";
      break;
   case WEBS_GETHOSTBYNAME:
	   ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_FIND_HOST,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
      pszStatus = "** finding host...\n";
      break;          
   case WEBS_CONNECT:
      pszStatus = "** connecting...\n";
	  ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_CONNECTING,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
      break;
   case WEBS_SENDREQUEST:
      pszStatus = "** sending...\n";
	  ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_SEND,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
      break;
   case WEBS_READRESPONSE:
      pszStatus = "** receiving...\n";
	  ISHELL_LoadResString(pApp->a.m_pIShell,DOWNLOADBMP_RES_FILE,IDC_RECEIVE,pApp->m_AnimalTitle,sizeof(pApp->m_AnimalTitle));
      break;
   case WEBS_GOTREDIRECT:
      pszStatus = "** redirect...\n";
      break;
   case WEBS_CACHEHIT:
      pszStatus = "** cache hit...\n";
      break;
   }

   // show that status!
   if ((char *)0 != pszStatus) {
     // ND_Print(pwa->pParent, "%s", pszStatus);
   }
}

/*===========================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -