/* CMPE4894 Project -- DoInet.cpp - set up connection with web server - send request to fetch the web page - receive and store data as a html file for parsing szUrl is as follow: when sample is build with OPENURL: URL to request, like http://www.microsoft.com */ TCHAR *lpCapture; TCHAR *lpObject; TCHAR DEFAULT[] = L"/"; void DoInet (LPTSTR szUrl, HWND hwnd) { HINTERNET hOpen, hReq; HANDLE hfile; TCHAR szMesg [256]; CHAR lpBuffTemp [32000], *lpHeaders; DWORD dwSum = 0, dwSize = 0, dwCode; DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE ; LPTSTR AcceptTypes[2]={L"*/*", NULL}, lpBuff; DWORD BytesWritten; //Set a handle for openning web page if ( !(hOpen = InternetOpen ( L"HttpDumpCE", INTERNET_OPEN_TYPE_PRECONFIG , NULL, 0, 0) ) ) { wsprintf (szMesg, L"InternetOpen Error: %d", GetLastError()); MessageBox (NULL, szMesg, L"Error", MB_OK); return ; } // Use OPENURL method to open the page // Set a handle for one connection, one web page may have several connections at a time if ( ! (hReq = InternetOpenUrl (hOpen, szUrl, NULL, 0, INTERNET_FLAG_RELOAD, 0 )) ) { wsprintf (szMesg, L"InternetOpenUrl Error: %d", GetLastError()); MessageBox (NULL, szMesg, L"Error", MB_OK); return ; } // Check HTTP status code to make sure that we have rights to access the // URL dwSize = sizeof (DWORD); if (!HttpQueryInfo (hReq, HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL)) { wsprintf (szMesg, L"HttpSendRequest Error: %d", GetLastError()); MessageBox (NULL, szMesg, L"Error", MB_OK); return ; } if (dwCode == HTTP_STATUS_DENIED || dwCode == HTTP_STATUS_PROXY_AUTH_REQ) { wsprintf (szMesg, L"WinInet on CE 2.1 does not support " L"authentication.\r\n" L"You will now see the error message as sent " L"by the server"); MessageBox (NULL, szMesg, L"Warning", MB_OK); } // retrieve request headers. Because we don't know // their size, we need to allocation memory first. First call to the // HttpQueryInfo will fail, but dwSize will contain the size of the // buffer we need to allocate. dwSize = 0; HttpQueryInfo (hReq,HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) NULL, &dwSize, NULL); lpHeaders = new CHAR [dwSize]; if (!HttpQueryInfo (hReq,HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpHeaders, &dwSize, NULL)) { wsprintf (szMesg, L"HttpQueryInfo Error: %d", GetLastError()); MessageBox (NULL, szMesg, L"Error", MB_OK); return ; } // Terminate headers with NULL. // Note: headers are returned as ASCII, we need to convert them to // UNICODE lpHeaders [dwSize] = '\0'; dwSize = MultiByteToWideChar(CP_ACP, 0, lpHeaders, -1, NULL, 0); lpBuff = new TCHAR [dwSize]; MultiByteToWideChar(CP_ACP, 0, lpHeaders, -1, lpBuff, dwSize); //create the Web page file hfile = CreateFile(TEXT("index.html"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if( hfile == INVALID_HANDLE_VALUE) { wsprintf (szMesg, L"CreateFile Error: %d", GetLastError()); MessageBox(NULL, szMesg, L"Error creating index.html", MB_OK); return ; } // Send headers to the edit control window SendMessage (hwnd, WM_READY_NOW, NULL, (LPARAM) lpBuff); SendMessage (hwnd, WM_READY_NOW, NULL, (LPARAM) L"\r\n"); delete[] lpBuff; delete[] lpHeaders; // Go the reading loop and start reading the data returned by the // HTTP Server. do { //Read web page data if (!InternetReadFile (hReq, (LPVOID) lpBuffTemp, 32000, &dwSize)) { wsprintf (szMesg, L"InternetReadFile Error: %d", GetLastError()); MessageBox (NULL, szMesg, L"Error", MB_OK); return ; } if (dwSize != 0) { // We have successfully read the chunk of data dwSum += dwSize; // change the count wsprintf (szMesg, L"Read: %d\n", dwSum); lpBuffTemp [dwSize] = '\0'; // 0 terminate it //try to write to our open file if(!WriteFile(hfile, lpBuff, dwSize, &BytesWritten, NULL)) { wsprintf (szMesg, L"WriteFile Error: tried to write %d bytes", dwSize); MessageBox (NULL, szMesg, L"Error writing to file", MB_OK); return ; } // convert the buffer from ASCII to UNICODE. // Because we don't know the needed size for UNICODE buffer, // we need to get its size first. MultiByteToWideChar(CP_ACP, 0, lpBuffTemp, -1, NULL, 0); lpBuff = new TCHAR [dwSize]; MultiByteToWideChar(CP_ACP, 0, lpBuffTemp, -1, lpBuff, dwSize); // by this time received chunk of data is UNICODE and ready to // be send to edit control SendMessage (hwnd, WM_READY_NOW, NULL, (LPARAM) lpBuff); delete[] lpBuff; } } while (dwSize); //close the newly created file CloseHandle(hfile); wsprintf (szMesg, L"\r\n\r\n -------------------- \r\n" L"Total read: %d", dwSum); SendMessage (hwnd, WM_READY_NOW, NULL, (LPARAM) szMesg); InternetCloseHandle (hOpen); InternetCloseHandle (hReq); }