Use SOCKET and mshtml object models to create your own browser

Source: Internet
Author: User
Tags socket connect
The birth of HTTP protocol and Web browser has added more highlights to our network. However, in actual applications, we may have different needs instead of simply using browsers. For example, we add the Internet browsing function to our applications. Microsoft's chtmlview class facilitates web browsing. However, it is not flexible, and users cannot dynamically modify the elements they want on the webpage. This article discusses some methods to use socket to transmit HTML documents, and use Microsoft's dynamic mshtml object model to implement some browser internal mechanisms.
As you know, HTML documents are composed of Markup languages, commonly known as tags. Microsoft's Browser IE implements a one-to-one object model (objectmodel) for these tags, which is encapsulated by mshtml. dll. The implementation of IE browser is also implemented by mshtml. dll. Through mshtml. dll, we can directly operate on the attributes and methods of the object model. The object model of mshtml is based on the COM component object, and the object interface is based on idispatch. To operate the mshtml object model, you must use the idispatch interface. Mshtml encapsulates many such interfaces. For example, the ihtmlanchorelement interface corresponds to the hyperjoin mark <A> and ihtmlhrelement interface <HR> mark in the HTML document, the ihtmltable interface corresponds to the <Table> mark. The most important is the ihtmldocument2 interface, which corresponds to the document component. The document component is equivalent to an HTML document. People who have used JavaScript should be familiar with it.
The following is an example of mshtml application. Before giving an example, let's talk about socket and HTTP. The HTTP protocol connects the server and client over TCP. It works on port 80. HTTP communicates through the request/response mechanism between the client and the server. HTTP messages are divided into request and response. Each message is composed of the start line, message header, and message body. The format is as follows:
Generic-message = start-line
* Message-Header
CRLF
[Message-body]

The start-line format is as follows:
Start-line = request-Line | status-line
Request-line is the request sent from the client to the server in the following format:
Request-line = method SP request-Uri SP http-version CRLF
Methods include get and post. In this example, we only use get to send requests to the server.
For more information about the HTTP protocol, see rfc2068.
Create a new single-document EXE project file in VC ++. In order to use mshtml, our view class inherits from chtmlview. The chtmlview class encapsulates the mshtml interface. Add a socket class to the project.
Class chttpsocket: Public csocket
{
..................

Protected:
Cwnd * m_pparentwnd;
}
M_pparentwnd points to our view class for message transmission. Define socket in the View class.
Class cskhttpview: Public chtmlview
{
Protected: // create from serialization only
Cskhttpview ();
Declare_dyncreate (cskhttpview)
................
Protected:
Chttpsocket m_socket;
Ihtmldocument2 * phmdoc2;
........
}
Phmdoc2 is the ihtmldocument2 interface. Initialize socke and connect to the site we want to log on to. Assume It is www.163.net.
Cskhttpview: cskhttpview ()
{
// Todo: Add construction code here
Bool Bret = m_socket.create (0, sock_stream, null );
If (! Bret)
MessageBox ("socket create error", null, mb_ OK );
M_socket.setparentwnd (this );
If (! M_socket.connect (www.163.net, 80 ))
MessageBox ("socket connect error", null, mb_ OK );
}
Next we get the ihtmldocument2 interface. There are two methods to obtain this interface. First, use cocreateinstance and then call QueryInterface. The other is to use the get_document of the mshtml control object, which encapsulates this interface in the chtmlview class. We use the latter one. Note that we need to obtain the interface after chtmlview generates the ihtmldocument object.
Void cskhttpview: oninitialupdate ()
{
Navigate2 ("about: blank ");
}
Void cskhttpview: ondocumentcomplete (lpctstr lpszurl)
{
Lpdispatch lpdisp;
Hresult hr;
Lpdisp = gethtmldocument ();
HR = lpdisp-> QueryInterface (iid_ihtmldocument2, (void **) & phmdoc2 );
}
Call navigate2 () to generate an empty document during chtmlview initialization. Call gethtmldocument to obtain an idispatch () interface and call QueryInterface to obtain the ihtmldocument2 interface.
Void cskhttpview: onreceivemessage (wparam, lparam)
{
Hresult hr;
Char Buf [5000];
Int inum;
Ihtmlelement * pelebody;
If (wparam = 0)
{
Inum = m_socket.receive (BUF, sizeof (BUF), 0 );
Buf [inum] = 0;
_ Bstr_t bsrbody (BUF );
HR = phmdoc2-> get_body (& pelebody );
HR = pelebody-> put_innerhtml (bsrbody );
}
}

Void cskhttpview: oncontrolsend ()
{
// Todo: add your command handler code here
Char Buf [1000];
Wsprintf (BUF, "Get http://www.163.net HTTP/1.1/R/n/R/N ");
Int iret = m_socket.send (BUF, lstrlen (BUF), 0 );
If (iret = socket_error)
MessageBox ("socket send error", null, mb_ OK );
}
We send the HTTP protocol request "Get http://www.163.net HTTP/1.1/R/n" to the server, which retrieves the webpage with the specified URI address. The server sends a response and we accept it through socket. Ihtmldocument2 contains many object excuses. Through many put _ and get _ methods, we can obtain the attributes, events, and methods of these objects. In this example, we obtain the body object, which corresponds to the <body> </body> object in the HTML document. Use get_body () of ihtmldocument2 to obtain the ihtmlelement interface of the specified body object. Call put_innerhtml () of the ihtmlelement interface to put the received content into the document. The webpage is displayed in our view.
This article briefly introduces the mshtml object, which actually contains many interfaces and functions. Through these interfaces, we can design our own browser. If you are interested, refer to the msdn document.

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.