Create a web browser using Win32 network functions

Source: Internet
Author: User
Tags gopher network function file transfer protocol

Create a web browser using Win32 network functions

Dale Rogerson

Microsoft Network Development Technical Team

Summary

This technical article discusses how to use Microsoft Win32 network functions to create a web browser. The purpose of this article is to let readers understand the functions, capabilities, and scope of use of some Win32 network functions, rather than providing a detailed document for these functions. The surfbear sample application used in this article uses Win32 network functions to read HTML files from the network server and display them as original, unformatted text.

 

Introduction

You cannot know a friend of mine without passing through the network. Computer magazines have already set up electronic journals on the Internet, and local newspapers have also put the entire section on the Internet. In fact, many newspapers are online. Everyone has a home page, and even some homeless people have a home page. Although there are many messages about the network, the network is becoming a part of the computer.

Microsoft has introduced Microsoft Win32 network functions to help developers turn networks into an integral part of their applications. These new features simplify the use of FTP (file transfer protocol), and HTTP (Hypertext Transfer Protocol) access networks. Developers who use Win32 network functions do not need TCP/IP or Windows accessories. For some of the most common operations, developers do not need to know the details of a protocol they are using.

In the end, Win32 network functions will become part of Win32 application interfaces and will be released together with different Windows-based platforms. Initially, Win32 network functions will be installed in a distributed dynamic connection library called wininet. dll. (From Microsoft network software development kit, which is on: http://www.microsoft.com/inter/sdle ). This is part of the Network Development Kit.

This article explains how to use Win32 network functions to create a simple web browser. It does not discuss the details of these functions in detail, but provides a demonstration of their usage and operations. For details, refer to the topic of Microsoft Win32 network functions at http://www.microsoft.com/intdev/sdk/docs/wininet.

This article was created in concert with the surfbear sample application. Surfbear is an HTML file. This process covers specific network parts, but it does not involve user interface problems related to this process or HTML file display or operation problems.

Note: This article is based on a fairly early version of wininet. dll. The parameter name, identifier name, and function name may change. However, the scope and intent of the function should be consistent with that described in this article.

 

Network functions

The best way to explore Win32 network functions is to directly access the code. The following code is sample code. To facilitate reading, the error handling part 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 );

: Internetclosehandle (hurlfile );

: Internetclosehandle (hnet );
The Code listed above includes four network functions: internetopen, internetopenorl, internetreadfile, and internetclosehandle. Next we will analyze these functions in sequence.

Internetopen

Internetopen initializes wininet. dll. It is called before other Win32 network functions.

Hinternet hnet =: internetopen (
"Msdn surfbear", // 1 lpctstr lpszcallername
Pre_config_internet_access, // 2 DWORD dwaccesstype
"", // 3 lpctstr lpszproxyname
Internet_invalid_port_number, // 4 internet_port nproxyport
0 // 5 DWORD dwflags
);
Internetopen returns a hinternet-type handle. Other Win32 network functions treat the handle as a parameter. Currently, you cannot use an hinternet handle in other Win32 functions such as readfile. However, with the maturity of Microsoft Windows and Microsoft Windows NT network support, this will not be impossible in the future.

When you have ended using wein32 network functions, you should call internetclosehandle to release the resources allocated by internetopen. Applications that use Microsoft basic class (MFC) will symbolically call internetopen from the file constructor. Most applications call internetopen in each process.

The first parameter of internetopen lpszcallername specifies the application that is using the network function. When the HTTP protocol is used, the name is changed to the user proxy.

The second parameter dwaccesstype specifies the access type. In the preceding example, the pre_config_internet_access access type indicates that Win32 network functions use registration information to discover a server. To use pre_config_internet_access, you must set the registration information correctly. Here I played a trick and asked network developers to register for me. If you do not want to cheat, set the registration information as shown in Figure 1.

 

 

 

 

In the registration process, setting accesstype to 1 means "directly accessing". Setting accesstype to 2 means "using gateway ". Setting disableservicelocation to 1 will make it use a named server; otherwise, a server that uses the registration information and name resolution (RnR) application interface will be found, it is part of the Windows interface.

Other access types include:

Local_internet_access only connects to the local Internet website. For example, if I use the surfbear flag, I can only access the overall Internet website of Microsoft.
Cern_proxy_internet_access uses a CERN proxy to access the Web. The CERN proxy is a web server that acts as a gateway and can send HTTP requests to the server that uses the proxy.
Gateway_internet_access allows you to connect to the World Wide Web. I can use this access type to access any website on the web.
Gateway_proxy_internet_access and cern_proxy_access require internetopen: Server Name (lpszproxyname) as the third parameter ). Pre_config_internet_access does not require the server name, because it can search for the storage information for the server.

The nproxyport parameter is used in cern_proxy_internet_access to specify the number of ports used. Internet_invalid_port_number is equivalent to the number of ports that are provided but saved.

Set extra options for the last parameter mongodwflags. You can use the internet_flag_async flag to indicate that future Internet functions that use the return sentence handle will send status information for the callback function. Use internetsetstatuscallback to perform this setting.

 

Internetopenurl

Once you initialize the Win32 network function, you can use other network functions. The next Internet function to be called is internetopenurl. This function is connected to a network server and the data is read most from the server. Internetopenurl can be used for FTP, Gopher, or HTTP. In this article, we only involve the HTTP protocol.

Hinternet hurlfile =: internetopenurl (
Hnet, // 1 hinternet hinternetsession
"Http://www.microsoft.com", // 2 lpctstr lpszurl
Null, // 3 lpctstr lpszheaders
0, // 4 DWORD dwheaderslength
Internet_flag_reload, // 5 DWORD dwflags
0 // 6 DWORD dwcontext
);
Internetopenurl also returns an hinternet, which is passed to the function that operates on this URL (Uniform resource location. You should use internetclose to close the processing of the handle.

The first parameter hinternetsession of internetopenurl is the handle returned from internetopen. The second parameter, lpszurl, is the URL of the resource we need (unified resource positioning ). In the above example, we want to get a Microsoft Web homepage. The following two parameters, lpszheaders and headerlength, are used to send additional information to the server. Using these parameters requires knowledge of specific protocols in use.

Dwflag is a sign that internetopenurl behavior can be modified in several ways. internetopenurl behavior includes closing and hiding, so that the original data can be used and an existing connection can be used 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 the value is 0, the information will not be sent to the status callback function.

 

Internetreadfile

After opening a file, you need to read it, so the next function is logical:

Char buffer [10*1024];
DWORD dwbytesread = 0;
Bool bread =: internetreadfile (
Hurlfile, // 1 hinternet hfile
Buffer, // 2 lpvoid lpbuffer
Sizeof (buffer), // 3 DWORD dwnumberofbytestoread
& Dwbytesread // 4 lpdword lpdwnumberofbytesread
);

Buffer [dwbytesread] = 0;
Peditctrl-> setwindowtext (buffer );
Internetreadfile receives the handle returned by internetopenurl. It also affects other Win32 network functions, such as ftpopenfile, fopheropenfile, and handle returned by httpopenrequest.

The three parameters of the remaining internetreadfile are also very straightforward. Inbuffer is a non-return value pointer pointing to the buffer with data retained. dwnumberofbytetoread specifies the buffer size in bytes. The last parameter, lpdwnumberofbytesread, is a pointer to a variable containing the number of bytes read into the buffer zone. If the returned value is true and lpdwnumberofbytesread points to 0, the object has read to the end of the object. This behavior is consistent with that of the Win32 re3adfile function. A real Web browser will loop through internetreadfile and read data blocks continuously from the Internet.

To display the buffer, add a 0 to the buffer and send it to the editor for control.

In this way, internetopen, internetopenurl, and internetreadfile create the foundation of the Internet browser. They make reading files from the Internet as easy as reading files from your local hard drive.

 

HTTP Functions

In some examples, internetopenurl is too common, so you may need other Win32 network functions. Internetopenurl is equivalent to the encapsulation of different FTP, Gopher, and HTTP functions. When HTTP is used, internetopenurl calls internetconnect, httpopenrequest, and httpsendrequest. For example, we want to obtain the size of an HTML page before downloading it so that we can allocate the appropriate size for it in the buffer zone, httpqueryinfo returns the size of the web page.

Warning not all web pages support page size. (For example, www.toystory.com and www.movielink.com do not support this function) In addition, TCP/IP can transmit less data than required. Therefore, your application should process two cases and loop around internetreadfile until the result is true and * lpdwnumberofbytesread is 0.

Use httpopenrequest, httpsendrequest, and httpqueryinfo to open the code of the file http://www.microsoft.com/msdn/msdninfoas shown below. The error detection has been deleted by mistake.

// 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,
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 bread =: internetreadfile (hhttpfile,
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 );

Internetconnect
The internetconnet function connects to an HTTP, FTP, or Gopher server:
Hinternet hconnect =: internetconnect (
Hsession, // 1 hinternet hinternetsession
"Www.microsoft.com", // 2 lpctstr lpszservername
Internet_invalid_port_number, // 3 internet_port nserverport
"", // 4 lpctstr lpszusername
"", // 5 lpctstr lpszpassword
Internet_service_http, // 6 DWORD dwservice
0, // 7 DWORD dwflags
O // 8 DWORD dwcontext
);
The sixth parameter dwservice determines the service type (HTTP, FTP, or gopher ). In the preceding 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 server address. Note that the HTTP address must be used for syntax analysis for the server name and internetopenurl for syntax analysis. The first hinternetsession parameter is the handle returned from internetopen. The fourth and fifth parameters provide a user name and password. These seven parameters do not control any flag that affects HTTP operations. The last parameter provides information about the frontend and backend relations of the status callback function.

Httpopenrequest
Once the connection to the server has been established, we open the desired file. Httpopenrequest and httpsenrequest work together to open the file. Httpopenrequest creates a request handle and stores parameters in the handle. Httpopenrequest sends the request parameters to the HTTP server.
Hinternet hhttpfile =: httpopenrequest (
Hconnect, // 1 hinternet hhttpsession
"Get", // 2 lpctstr lpszverb
"/Msdn/msdninfo/", // 3 lpctstr lpszobjectname
Http_version, // 4 lpctstr lpszversion
Null, // 5 lpctstr lpszreferer
0, // 6 lpctstr far * lplpszaccepttypes
Internet_flag_dont_cache, // 7 DWORD dwflags
0 // 8 DWORD dwcontext
);
So far, many parameters of network functions seem similar. The first parameter of httpopenresult is the hinternet returned by internetconnet. The seventh and eighth parameters of httpopenrequest perform the same function as internetconnect.
The second parameter ("get") specifies the object we want to get named by the third parameter ("/msdn/msdninfo. HTTP has passed the fourth parameter. Now, it must be HTTP Protocol version. Because "get" is the most popular verb type, httpopenrequest receives a null pointer for this parameter.
The fifth parameter lpszreferer is the address of a network. On this site, we found the URL we want to see now (unified resource positioning ). In other words, if you click a connection on www.home.com and jump to www.microsoft.com, the fifth parameter is www.home.com. Because it directs you to the target URL (Uniform Resource positioning ). This value can be empty. The sixth parameter executes a list of file types received by our program. Pass the null value to httpopenrequest to notify the server that only text files can be received.

 

Httpsendrequest

In addition to sending requests, httpsendrequest allows you to send additional HTTP headers to the server. Information about HTTP headers can be found in the latest description on the http://www.w3.org. In this example, all httpsendrequest parameters are passed as default values.

Bool bsendrequest =: httpsendrequest (
Hhttpfile, // 1 hinternet hhttprequest
Null, // 2 lpctstr lpszheaders
0, // 3 DWORD dwheaderslength
0, // 4 lpvoid lpoptional
0 // 5 DWORD dwoptionallength
);
Httpqueryinfo

To obtain information about the file, use the httpqueryinfo function after httpsendrequest is called:

Bool bquery =: httpqueryinfo (
Hhttpfile, // 1 hinternet hhttprequest
Http_query_content_length, // 2 DWORD dwinfolevel
Bufquery, // 3 lpvoid lpvbuffer
& Dwlengthbufquery // 4 lpdword lpdwbufferlength
);
The query structure is the string list in the string or lpvbuffer. Http_query_content_length: query the file length. You can use httpqueryinfo to query a wide range of information. For details, refer to the Microsoft Win32 network function topic on http://www.microsoft.com/intdev/sdk/docs/wininet.

Surfbear Sample Application

Surbear sample applications use Win32 network functions to obtain files from the Internet and display the original HTML format in the editor. Surfbear replaces internetopenurl with httpopenrequest and httpsendrequest, purely for demonstration purposes.

 

Figure 2 surfbear Screen

Surfbear is a dialog application of mfc4.0. All Internet-related functions are included in the internetthread. h and internetthread. cpp files.

Reading files from the Internet takes a considerable amount of time, so it is wise to call a network function from a working thread. In this way, the application window can be changed and moved when the system is waiting for data to be obtained.

Figure 3 shows the control flow of surfbear.

 

 

When you press the Goto button, csurfbeardlg: onbtngoto calls cinternetthread: getwebpoge to pass the HTTP address of the desired webpage. Getwebpage analyzes the HTTP address syntax into the server and object names and stores them in the member variables of cinternetthread. Getwebpage then calls afxbeginthread to generate the getwebpage workerthread thread that runs the static member function. If the network function is not initialized, getwebpageworkerthread calls internetopen and then tries to read the desired webpage. When getwebpageworkerthread ends, it sends a user-defined wm_readfilecompleted message to the surfbear dialog box. Onreadfilecompleted processes the message and copies a web page to the editor control.

Summary

Win32 network functions make it as easy to read information from ftp, Gopher, and HTTP servers as you want to read information from your hard drive. You can write a simple Web browser by using only four functions: internetopen, internetopenurl, internetreadfile, internetclosehandle, and little HTTP knowledge.

It takes a lot of work to turn this simple browser into an industrial browser, including some understanding of HTTP, how to display HTML files, and the ability to use multithreading. Win32 network functions free developers from most boring work related to TCP/IP, Windows Sockets, and HTTP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.