#include <windows.h>
#include <wininet.h>
#define  MAX_FILE_SIZE  8192#define HTTPSERVER "http://www.google.com/default.htm"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int DownloadThisFile(char *,char *,DWORD,DWORD ); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("Test") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
 
     if (!RegisterClass (&wndclass))
     {
 MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
 szAppName, MB_ICONERROR) ;
 return 0 ;
     }
 
     hwnd = CreateWindow (szAppName, TEXT ("Test"),
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT, CW_USEDEFAULT,
 CW_USEDEFAULT, CW_USEDEFAULT,
 NULL, NULL, hInstance, NULL) ;
 
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
 
     while (GetMessage (&msg, NULL, 0, 0))
     {
 TranslateMessage (&msg) ;
 DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}int DownloadThisFile(
 char WebUrl[], // name of remote file to download using HTTP
 char FileContentsBuffer[],
 DWORD dwNumberOfBytesToRead,
 DWORD dwNumberOfBytesRead
 )
{
int iReturnValue;    DWORD dwErrorCode = NO_ERROR;
HINTERNET hInet,hFile;    // -------------------------------------------------------
    // Open the internet session:
    hInet = InternetOpen(TEXT("Test"), INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    if (NULL == hInet)
    {
        dwErrorCode = GetLastError();
      
        return FALSE;
    }

    // -------------------------------------------------------
    // go to the desired URL:
hFile = InternetOpenUrl(hInet,WebUrl,NULL,0,0,0);    if (NULL == hFile)
    {
        dwErrorCode = GetLastError();  //出错了! 12006
        InternetCloseHandle(hInet);
        return FALSE;
    }

    // -------------------------------------------------------
    // read the file:
    iReturnValue = InternetReadFile(hFile,(LPVOID) FileContentsBuffer,
        dwNumberOfBytesToRead,&dwNumberOfBytesRead);

    // -------------------------------------------------------
    InternetCloseHandle(hFile);
    InternetCloseHandle(hInet);
 
return iReturnValue;
}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

HDC         hdc ;
PAINTSTRUCT ps ;
char       szBuffer [256] ;
char szFile[16*1024]; RECT      rect;
DWORD     dwErrorCode = NO_ERROR;
// char     szURL[MAX_PATH];
HWND      hButton;
int       iDraw = FALSE ;    DWORD dwNumberOfBytesToRead = MAX_FILE_SIZE;
    DWORD dwNumberOfBytesRead = 0; switch (message)
{
case WM_CREATE:
hButton = CreateWindow(TEXT("button"),TEXT("Connect"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
10,10,60,20,hwnd,(HMENU)0,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),0);

return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ; if(iDraw)
{
SetRect(&rect,0,40,400,300);
DrawText(hdc,szFile,strlen(szFile),&rect,DT_LEFT|DT_WORDBREAK);
}

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_COMMAND: 
DownloadThisFile(HTTPSERVER,szFile,dwNumberOfBytesToRead,dwNumberOfBytesRead);
iDraw = TRUE;
return 0; case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
为什么上面那个程序不行????

解决方案 »

  1.   

    ALT+F7,link,object/libaray module 加上Wininet.lib
      

  2.   

    或者#pragram comment(lib, "Dbghelp.lib")
      

  3.   

    #pragram comment(lib, "Wininet.lib")
      

  4.   

    不缺库
    InternetOpenUrl
    是这里出错了!
    错误代码是12006
      

  5.   

    ERROR_INTERNET_UNRECOGNIZED_SCHEME 
    The URL scheme could not be recognized, or is not supported. 
      

  6.   

    你的URL写的是不是有问题啊?
      

  7.   

    HINTERNET hNet = ::InternetOpen("MSDN SurfBear",
                                    PRE_CONFIG_INTERNET_ACCESS,
                                    NULL,
                                    INTERNET_INVALID_PORT_NUMBER,
                                    0) ;HINTERNET hUrlFile = ::InternetOpenUrl(hNet,
                                    "http://www.microsoft.com",
                                    NULL,
                                    0,
                                    INTERNET_FLAG_RELOAD,
                                    0) ;char buffer[10*1024] ;
    DWORD dwBytesRead = 0;
    BOOL bRead = ::InternetReadFile(hUrlFile,
                                    buffer,
                                    sizeof(buffer),
                                    &dwBytesRead);::InternetCloseHandle(hUrlFile) ;::InternetCloseHandle(hNet) ;