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

📄 cehttp.cpp

📁 WINDOWS CE 通信指南(附光盘) 本书配套光盘内容包括:本书的英文版电子书;SDK for Windows CE
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************

THIS CODE AND INFORMATION IS PROVIDED AS IS WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.

Copyright(c)  1999  Microsoft Corporation.  All Rights Reserved.

MODULE: 
  CeHttp.cpp

ABSTRACT:  
  This sample application demonstrates how to create and submit a HTTP 
  request. It requests the default HTML document from the server and 
  then display it along with the HTTP transaction headers.

***********************************************************************/

#include <windows.h>
#include <windowsx.h>
#include <wininet.h>
#include <commctrl.h>
#include "resource.h"
  
#define   ID_EDIT             100
#define   WM_PUTTEXT          WM_USER + 1

HINSTANCE g_hInst = NULL;     // Handle to the application instance
HWND g_hwndMain = NULL;       // Handle to the application main window
HWND g_hwndCB = NULL;         // Handle to the command bar
HWND g_hwndEdit = NULL;       // Handle to the Edit Control window
  
TCHAR g_szTitle[80];          // Main window name
TCHAR g_szClassName[80];      // Main window class name

TCHAR g_szCaption[MAX_PATH];  // Caption of the application main window
TCHAR g_szIAddName[MAX_PATH]; // Name and path of the current file
TCHAR g_szProxySvr[MAX_PATH]; // Name of the proxy server
                              // For example: TEXT("http://proxy:80")
BOOL g_bProxy = FALSE,
     g_bOpenURL = FALSE;


/***********************************************************************

FUNCTION: 
  GetInternetFile

PURPOSE: 
  This function demonstrates how to create and submit a HTTP request.
  It requests the default HTML document from the server and then display
  it along with the HTTP transaction headers.
 
***********************************************************************/
BOOL GetInternetFile (LPTSTR lpszServer, LPTSTR lpszProxyServer)
{
  BOOL bReturn = FALSE;

  HINTERNET hOpen = NULL, 
            hConnect = NULL, 
            hRequest = NULL;

  DWORD dwSize = 0, 
        dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE; 

  TCHAR szErrMsg[200];
  
  char *lpBufferA,
       *lpHeadersA;

  TCHAR *lpBufferW,
        *lpHeadersW;

  LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL}; 

 
  // Initialize the use of the Windows CE Internet functions.
  if (g_bProxy)
  {
    hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PROXY, 
                          lpszProxyServer, 0, 0);
  }
  else
  {
    hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PRECONFIG,
                          NULL, 0, 0);
  }

  if (!hOpen)
  {
    wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpen Error"), 
              GetLastError());
    return FALSE;
  }
  
  if (g_bOpenURL)
  {
    if (!(hRequest = InternetOpenUrl (hOpen, lpszServer, NULL, 0, 
                                      INTERNET_FLAG_RELOAD, 0)))
    {
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpenUrl Error"),
                GetLastError());
      goto exit;
    }
  }
  else
  {
    // Open a HTTP session for a given site by lpszServer. 
    if (!(hConnect = InternetConnect (hOpen, 
                                      lpszServer, 
                                      INTERNET_INVALID_PORT_NUMBER, 
                                      NULL, NULL, 
                                      INTERNET_SERVICE_HTTP, 
                                      0, 0)))
    {
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
                GetLastError());
      goto exit;
    }

    // Open a HTTP request handle. 
    if (!(hRequest = HttpOpenRequest (hConnect, 
                                      TEXT("GET"), 
                                      NULL, 
                                      HTTP_VERSION, 
                                      NULL, 
                                      (LPCTSTR*)AcceptTypes, 
                                      dwFlags, 0)))
    {
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpOpenRequest Error"),
                GetLastError());
      goto exit;
    }

    // Send a request to the HTTP server. 
    if (!HttpSendRequest (hRequest, NULL, 0, NULL, 0))
    {
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpSendRequest Error"),
                GetLastError());
      goto exit;
    }
  }
  
  // Call HttpQueryInfo to find out the size of the headers.
  HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
                 NULL);

  // Allocate a block of memory for lpHeadersA.
  lpHeadersA = new CHAR [dwSize];

  // Call HttpQueryInfo again to get the headers.
  if (!HttpQueryInfo (hRequest, 
                      HTTP_QUERY_RAW_HEADERS_CRLF, 
                      (LPVOID) lpHeadersA, &dwSize, NULL))
  {
    wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpQueryInfo"), 
              GetLastError());
    goto exit;
  }
  else
  {
    // Clear all of the existing text in the edit control and prepare  
    // to put the new information in it.
    SendMessage (g_hwndEdit, EM_SETSEL, 0, -1);
    SendMessage (g_hwndEdit, WM_CLEAR, 0, 0);
    SendMessage (g_hwndEdit, WM_PAINT, TRUE, 0);
  }

  // Terminate headers with NULL.
  lpHeadersA [dwSize] = '\0';
  
  // Get the required size of the buffer that receives the Unicode 
  // string. 
  dwSize = MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, NULL, 0);
  
  // Allocate a block of memory for lpHeadersW.
  lpHeadersW = new TCHAR [dwSize];

  // Convert headers from ASCII to Unicode
  MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, lpHeadersW, dwSize);  
  
  // Put the headers in the edit control.
  SendMessage (g_hwndMain, WM_PUTTEXT, NULL, (LPARAM) lpHeadersW);
   
  // Free the blocks of memory.
  delete[] lpHeadersA;
  delete[] lpHeadersW;

  // Allocate a block of memory for lpHeadersW.
  lpBufferA = new CHAR [32000];

  do
  {
    if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, 32000, &dwSize))
    {
      wsprintf(szErrMsg, TEXT("%s: %x"), TEXT("InternetReadFile Error"), 
               GetLastError());
      goto exit;
    }

    if (dwSize != 0)    
    {
      // Terminate headers with NULL.
      lpBufferA [dwSize] = '\0';                 

      // Get the required size of the buffer which receives the Unicode
      // string. 
      dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);
      
      // Allocate a block of memory for lpBufferW.
      lpBufferW = new TCHAR [dwSize];
      
      // Convert the buffer from ASCII to Unicode.
      MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);  

      // Put the buffer in the edit control.
      SendMessage (g_hwndMain, WM_PUTTEXT, NULL, (LPARAM) lpBufferW);      

      // Free the block of memory.
      delete[] lpBufferW;  
    }    
  } while (dwSize);

  // Free the block of memory.
  delete[] lpBufferA;  

  bReturn = TRUE;

exit:

  // Close the internet handles.
  if (hOpen)
  {
    if (!InternetCloseHandle (hOpen))
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
                GetLastError());
  }
  
  if (hConnect)
  {
    if (!InternetCloseHandle (hConnect))
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
                GetLastError());
  }

  if (hRequest)
  {
    if (!InternetCloseHandle (hRequest))
      wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
                GetLastError());
  }

  return bReturn;
}

/***********************************************************************

FUNCTION: 
  AboutDialogProc

PURPOSE: 
  Processes messages sent to the About dialog box window.

***********************************************************************/
BOOL CALLBACK AboutDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, 
                               LPARAM lParam)  
{
  switch (uMsg)
  {
    case WM_INITDIALOG:
      return TRUE;  

    case WM_COMMAND:
      switch (LOWORD (wParam))
      {
        case IDOK:
          EndDialog (hwndDlg, IDOK);
          return TRUE;

        case IDCANCEL:
          EndDialog (hwndDlg, IDCANCEL);
          return TRUE;
      }
      break;
  }
  return FALSE;
}

    
/***********************************************************************

FUNCTION: 
  DialogProc

PURPOSE: 
  Processes messages sent to the Internet Address dialog box window.

⌨️ 快捷键说明

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