Create a web browser with Win32 network functions

zhaozj2021-02-08  237

Create a web browser Dale Rogersonmicrosoft Network Development Technology Group Summary This technical article discusses how to create a web browser using the Microsoft Win32 network function. The purpose of this article is to let readers understand some of the roles, capabilities and use of Win32 network functions, rather than give a detailed document for these features. The SURFBEAR sample application in this article uses the Win32 network function to read the HTML file from the web server and display them into original, no formatted text. Introduce the network, you can't understand my friend. Computer Magazine has already set up electronic journals on the Internet, and local newspapers have also put all paragraphs in the network. In fact, many newspapers are online. Everyone has a homepage, and even some homeless people have a homepage. Although there are many news about the network, it is difficult to say in fact, but the network is becoming a part of the overall computer. Microsoft has already introduced Microsoft Win32 network functions to assist developers turn networks into the overall part of their application. These new features simplify the use of FTP (File Transfer Protocol), and HTTP (Hyper Text Transfer Protocol) to access the network. Developers using Win32 network functions do not need TCP / IP or Windows accessories. For some most ordinary operations, developers don't need to know the details of a certain agreement they are using. Finally, the Win32 network function will become part of the Win32 application interface and is released with Windows-based different platforms. Initially, the Win32 network function will be installed in a redistributed dynamic link library called Wininet.dll. (From the Microsoft Network Software Development Kit, its URL is: http://www.microsoft.com/inter/sdle/). This is part of the network development kit. This article shows how to create a simple web browser using the Win32 network function. It does not specifically discuss details of these functions, but gives a presentation for their usage and operation. Please refer to the topic of the Microsoft Win32 network function of http://www.microsoft.com/intdev/sdk/docs/wininet, you can understand all details. This article is created with the Surfbear sample application. Surfbear is an HTML file. It covers the specific network part of this process, but it does not involve the display or operation of the user interface problem or HTML file related to this process. Note: This article is based on Wininet.dll a fairly early version. It is very likely that the parameter name, the identification name, and the function name have changed. But the range and intent of the function should still be consistent with this article. The best way to discuss the Win32 network function is to enter the code directly. The following code is the code of the sample, in order to facilitate reading, the error handling section has been deleted.

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); :: (hUrlFile) InternetCloseHandle; :: (hNet) InternetCloseHandle; exemplified above The code includes four network functions: Internetopen, InternetOpenorl, InternetReadFile, and InternetCloseHandle. Here we analyze these functions in turn. InternetopenInternetopen initializes Wininet.dll. It is called before other Win32 network functions. HINTERNET hNet = :: InternetOpen ( "MSDN SurfBear", // 1 LPCTSTR lpszCallerNamePRE_CONFIG_INTERNET_ACCESS, // 2 DWORD dwAccessType "", // 3 LPCTSTR lpszProxyNameINTERNET_INVALID_PORT_NUMBER, // 4 INTERNET_PORT nProxyPort0 // 5 DWORD dwFlags); InternetOpen returns a type HINTERNET Handle. Other Win32 network functions use this handle as a parameter. Now you can't use a HinterNet handle in other Win32 functions similar to the readfile. But with the maturity of Microsoft Windows and Microsoft Windows NT networks, this is not impossible in the future. When you have ended using a Wein32 network function, you should call InternetCloseHandle to release the Internet allocated resources. Use the Microsoft Base Class (MFC) application to call Internetopen from the file constructor. Most applications will call Internetopen in each process. Internetopen's first parameter lpszcallerName Specifies the application that is using the network function. This name will become a user agent when the HTTP protocol is used. The second parameter dwaccesstype specifies the access type. In the above example, the pre_config_internet_access access type indicates that the WIN32 network function uses the registration information to discover a server. Use pre_config_internet_access to set registration information correctly. Here I play a small trick and let the network developer register for me. If you don't want to deceive, you need to set registration information as shown in Figure 1. In the registration registration, set AccesSstype to 1, which means "direct access", set AccesSstype to 2, meaning "using gateways". Set DisableServiceLocation to 1, which will make it use a named server; otherwise you will find a server using the Registration Information and Name Resolution (RNR) application interface, which is part of the Windows interface. Other access types include the following: local_internet_access is only connected to the local Internet website.

For example, if I use the Surfbear flag, I can only access Microsoft's overall Internet website. CERN_PROXY_INTERNET_ACCESS uses a CERN agent to access the web. Cern agent is a web server that acts as a gateway and can send HTTP requests to servers to use the agent. Gateway_Internet_access allows you to connect to World Wide Web. I can use this access type to access any sites on the Web. Gateway_proxy_internet_access and cern_proxy_access Access Type Requirements The third parameter gives the Internetopen: server name (LPSZPROXYNAME). Pre_config_internet_access does not require the server name because he can search for the server. The NProxyPort parameter is used to specify the number of ports used in CERN_PROXY_INTERNET_ACCESS. Use Internet_invalid_port_number equivalent to the number of ports that provide The last parameter dwflags set additional options. You can use the Internet_flag_async flag to indicate the future Internet function using the return clause will send status information to the callback function and use the InternetStatusCallback to make this setting. Internetopenurl Once you initialize the Win32 network function, you can use other network functions. The next Internet function to call is Internetopenurl. This function is connected to a web server and is most read from the server. InternetopenURL works on FTP, GOPHER, or HTTP protocol. In this article, we only involve the HTTP protocol. HINTERNET hUrlFile = :: InternetOpenUrl (hNet, // 1 HINTERNET hInternetSession "http://www.microsoft.com", // 2 LPCTSTR lpszUrlNULL, // 3 LPCTSTR lpszHeaders0, // 4 DWORD dwHeadersLengthINTERNET_FLAG_RELOAD, // 5 DWORD dwFlags0 / / 6 DWORD DWCONTEXT); Internetopenurl also returns a Hinternet that is passed to a function that operates on this URL (Uniform Resource Location). You should use InternetClose to close the handle of this handle. The first parameter of Internetopenurl HinternetSession is a handle returned from Internetopen. The second parameter lpszurl is the URL of the resource we need. In the above example, we want to get a Microsoft's web home page. The following two parameters lpszheaders and headerLength are used to deliver additional information to the server. Use these parameters to require knowledge of specific protocols that are using. DWFlag is a sign that can modify the Internetopenurl behavior in several ways, including closing, hidden, so that the original data can be used and replaced with existing connections to open up a new connection. The last parameter dwconText is a DWORD context value. If a value has been specified, it will be sent to the status callback function. If this value is 0, the information will not be sent to the status callback function.

After InternetReadFile, you have to read it, so the next function is internetReadFile is logical: char buffer [10 * 1024]; dword dwbytesread = 0; Bool Bread = :: InternetReadfile (HURLFILE, // 1 Hinternet HfileBuffer , // 2 LPVOID lpBuffersizeof (buffer), // 3 DWORD dwNumberOfBytesToRead & dwBytesRead // 4 LPDWORD lpdwNumberOfBytesRead); buffer [dwBytesRead] = 0; pEditCtrl-> SetWindowText (buffer); InternetReadFile InternetOpenUrl receiving the returned handle. It also affects other Win32 network functions, such as ftpopenfile, Fopheropenfile, and HTTPOPENREQUEST returned. The three parameters of the remaining InternetReadFile are also very clear. Inbuffer is a non-return value pointer to the buffer that retains the data, and dwnumberofbyteetore specifies the size of the buffer in bytes in bytes. The last parameter, LPDWNumberofBytesRead is a pointer to the number of variables that contain the number of read buffers. If the return value is true, and LPDWNumberofBytesRead points to 0, the file has been read at the end of the file. This behavior is consistent with the behavior of the function of Win32 Re3adfile. A real web browser will loop on the InternetReadFile and do not stop from the Internet. To display the buffer, add 0 to the buffer and send it to the editor control. Thus, Internetopen, Internetopenurl and InternetReadFile have created the foundation of the Internet browser. They enable reading files from the Internet as easy as read from your local hard drive. In some examples, the HTTP function is too common, so you may need other Win32 network functions. Internetopenurl is quite with different FTP, Gopher and HTTP functions. When using HTTP, InternetopenURL calls InternetConnect, HttPopenRequest, and httpsendRequest, for example, we want to get its size before downloading an HTML page to facilitate our assignment in the buffer, HttpQueryInfo will get the size of the web page. WARNING: Not all web pages support getting the page size. (For example, www.toystory.com and www.movielink.com does not support this feature) In addition, the data that TCP / IP can pass is less than the requirements. So, your application should handle two situations and circulate around the InternetReadFile until the result is true * LPDWNUMBEROFBYTESREAD is 0. Use httpopenRequest, httpsendrequest, and httpqueryinfo to open the file http://www.microsoft.com/msdn/msdninfo code shows the following, error detection has been deleted.

// Open Internet session.HINTERNET hSession = :: InternetOpen ( "MSDN SurfBear", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); // Connect to www.microsoft.com.HINTERNET hConnect = :: InternetConnect (hSession, "www.microsoft .com ", Internet_invalid_port_number,", ", Internet_service_http, 0); // Request the file / msdn / msdninfo / from the server.hinternet hhttpfile = :: httpopenrequest (hconnect," get "," / msdn / MSDNINFO / ", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); // Send the request.BOOL bSendRequest = :: HttpSendRequest (hHttpFile, NULL, 0, 0, 0); // Get the length of the file char bufQuery. [32]; DWORD dwLengthBufQuery = sizeof (bufQuery); BOOL bQuery = :: HttpQueryInfo (hHttpFile, HTTP_QUERY_CONTENT_LENGTH, bufQuery, & dwLengthBufQuery); // Convert length from ASCII string to a DWORD.DWORD dwFileSize = (DWORD) atol (bufQuery); // allocate a buffer for the file.char * buffer = new char [dwfilesize 1]; // read the file inTo the buffer.dword dwbytesread; bool break = :: InternetreadFile (hhtt pFile, buffer, dwFileSize 1, & dwBytesRead); // Put a zero on the end of the buffer.buffer [dwBytesRead] = 0; // Close all of the Internet handles :: InternetCloseHandle (hHttpFile);. :: InternetCloseHandle ( hConnect); :: InternetCloseHandle (hSession); // Display the file in an edit control.pEditCtrl-> SetWindowText (buffer); InternetConnectInternetConnet a function to connect to HTTP, FTP or Gopher server: HINTERNET hConnect = :: hSession InternetConnect (, / / 1 Hinternet HinternetSession "www.microsoft.com", // 2 lpctstr lpszservernameInternet_invalid_port_number, // 3 internet_port nserverport "", // 4 lpctstr lpszusername ""

, // 5 LPCTSTR LPSZPASSWORDISTERNET_SERVICE_HTTP, / / ​​6 DWORD DWSERVICE0, // 7 DWORD DWFLAGSO / / 8 DWORD DWCONTEXT); The sixth parameter dwservice determines the service type (HTTP, FTP or GOPHER). In the above example, InternetConnect is connected to an HTTP server because DWService is set to internet_service_http. The second parameter (set to www.microsoft.com) provides the address of the server. Note that the HTTP address must be a syntax analysis of the server, and Internetople is a grammatical analysis. The first parameter HinternetSession is the handle returned from Internetopen. The fourth, the fifth parameter provides a user name and password. These seven parameters do not control any flag affecting HTTP operations. The last parameter provides information on the front and rear relationships for the status callback function. Once the HTTPOPENREQUEST has been established and the connection is established, we open the desired file. HTTPOPENREQUEST and HTTPSENREQUEST work together to open the file. HttPopenRequest creates a request handle and stores the parameters in the handle. HttPopenRequest sent the request parameters to the HTTP server. HINTERNET hHttpFile = :: HttpOpenRequest (hConnect, // 1 HINTERNET hHttpSession "GET", // 2 LPCTSTR lpszVerb "/ MSDN / MSDNINFO /", // 3 LPCTSTR lpszObjectNameHTTP_VERSION, // 4 LPCTSTR lpszVersionNULL, // 5 LPCTSTR lpszReferer0, / / 6 LPCTSTR FAR * LPLPSZACCEPTTTYPESINTERNET_FLAG_DONT_CACHE, / / ​​7 DWORD DWFLAGS0 // 8 DWORD DWCONTEXT); Many of the parameters of the network function look similar. The first parameter of HttPopenResult is the HinterNet returned by InternetConnet. The seventh and eighth parameters of HTTPopenRequest execute the same functionality as the same name in InternetConnect. The second parameter ("GET") specifies that we want to get an object named by the third parameter ("/ msdn / msdninfo /"). The HTTP version has passed the fourth parameter; now it is definitely http jujube version. Because "get" is the most popular verb type, HttPopenRequest will receive an empty pointer for this parameter. The fifth parameter lpszreferer is the address of a network. At this point we found the URL (unified resource positioning) we want to see now. In other words, if you are on www.home.com and click on a connection to www.microsoft.com, the fifth parameter is www.home.com. Because it makes you point to the target URL (unified resource positioning). This value can be empty. The sixth parameter performs a list of file types received by our program. Pass null values ​​to HTTPOPENREQUEST to notify the server only text files can be received. In addition to the transfer request, HttpsendRequest allows you to deliver additional HTTP titles to the server. Information about HTTP titles can be found on the latest instructions on http://www.w3.org/. In this example, all parameters of HttpsendRequest are passed to the default.

BOOL bSendRequest = :: HttpSendRequest (hHttpFile, // 1 HINTERNET hHttpRequestNULL, // 2 LPCTSTR lpszHeaders0, // 3 DWORD dwHeadersLength0, // 4 LPVOID lpOptional0 // 5 DWORD dwOptionalLength); HttpQueryInfo order to obtain information about a file, call HttpSendRequest after use HttpQueryInfo function: BOOL bQuery = :: HttpQueryInfo (hHttpFile, // 1 HINTERNET hHttpRequestHTTP_QUERY_CONTENT_LENGTH, // 2 DWORD dwInfoLevelbufQuery, // 3 LPVOID lpvBuffer & dwLengthBufQuery // 4 LPDWORD lpdwBufferLength); structure of the query string or strings lpvBuffer List. HTTP_QUERY_CONTENT_LENGTH Query The length of the file is obtained. You can use HttpQueryInfo to query a wide range of information. To get aware of the details of the website http://www.microsoft.com/intdev/sdk/docs/wininet. SURFBEAR Sample Application Surbear Sample Application Use the Win32 network function to get the file from the Internet and display the original HTML format on the editor. Surfbear uses HttPopenRequest and HttpsendRequest to replace Internetopenurl, purely for demands. Figure 2 SurfBear screen SurfBear is a MFC4.0 version of the dialog application. It is all in the InternetthRead.h and the Internet. It takes a considerable amount of time from the Internet to read files, so calling network functions from a work thread is a wise idea. In this way, when the system is waiting for data, the application's window can be changed and moved. Figure 3 shows the control flow of the Surfbear. When the user presses the Goto button, csurfbeardlg :: Onbtngoto calls CinternetThread: getWebPoge, passes the HTTP address of the desired web page. GetWebPage analyzes the HTTP address syntax into servers and object names, stores in member variables of CinternetThread. GetWebPage then calls AFXBEGINTHREAD, which generates a thread GetWebPage Workerthread running a static member function. If the network function is not initialized, GetWebPageWorkeRThread calls Internetopen, then it tries to read the desired web page. When GetWebPageWorkeRThread ends, it sends a user-defined WM_READFileCompleted message to the Surfbear dialog. OnReadFileCompleted processes this message and copy a web page to the editor control. Summary The Win32 network function makes it easy to read information on your hard drive from the FTP, GOPHER, and HTTP servers. Just use 4 functions Internetopen, InternetopenuRl, InternetReadFile, and INTERNETCLOSEHANDLE and very few HTTP knowledge, you can write a simple web browser. The browser that turns this simple browser into an industrial nature will cost a lot of work, including some understanding of HTTP, how to display the HTML files and the ability to use multithreading.

转载请注明原文地址:https://www.9cbs.com/read-2858.html

New Post(0)