Create your own browser with Socket and MSHTML object model

zhaozj2021-02-11  206

Create your own browser with Socket and MSHTML object model

Xue Wei

The birth of the HTTP protocol and the birth of the web browser add more exciting to our network. But in practical applications, we may have different needs instead of simply use browsers, such as the ability to browse the Internet in our application. Microsoft's ChtmlView class is very convenient to achieve web browsing. But it is very unhappy, and it is unable to dynamically modify the elements you want on the web page. This article explores some methods to use Socket to transfer HTML documents, using Microsoft's dynamic MSHTML object model to implement some browser internal mechanisms. Everyone knows that the HTML document is made by marker language, that is, commonly known as TAG. Microsoft's browser IE implements a corresponding object model (ObjectModel) for these tags, encapsulated by MSHTML.DLL. The implementation of the IE browser is also implemented by mshtml.dll. With mshtml.dll, we can directly operate the properties and methods of the object model. The object model of the MSHTML is based on the COM component object. The object's interface is based on iDispatch, and the operation MSHTML object model must pass the IDispatch interface. Many of the interfaces are encapsulated in MSHTML, for example, the IHTMLANChorElement interface corresponds to the


tag, the IHTMLHleMLEMENT interface corresponding to the HTML document, and the IHTMLTable interface corresponds to the tag. The most important thing is the IHTMLDocument2 interface, which corresponds to the Document component. The Document component is equivalent to the HTML document. People who have used JavaScript will be familiar with it. Let us explain the application of MSHTML. Before an example, I will tell the Socket and HTTP protocol. The HTTP protocol is connected to the server and client through the TCP, which works at 80 ports. HTTP communicates with the request / response mechanism between Client and Server. HTTP messages are divided into request and response. Each message consists of starting line, a message header, and a message. The form is as follows: generic-message = start-line * message-header crlf [message-body]   START-line = request-line | status-line request-line is a request sent to Server to server. The form is as follows: request-line = method sp request-URI SP HTTP-VERSION CRLFMETHOD includes GET, POST, etc. In this case, we only use GET to send a request to the server. For detailed HTTP protocol, please refer to RFC2068. Create a new single document EXE project file in VC , in order to inherit from CHTMLVIEW with MSHTML. The CHTMLVIEW class encapsulates the MSHTML interface. Add a Socket class to the project. Class Chttpsocket: public csocket {.................. protected: cwnd * m_pparentWnd;} where m_pparentWnd points to our view class for transmitting messages. Define the 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 IHTMLDocument2 interface. Initialize Socke, connect our site to log in, assume

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);} Let's get the interface of IHTMLDocument2. There are generally two methods for this interface. First, use CoCreateInstance and call QueryInterface. The other is Get_Document using the MSHTML control object, and this interface is encapsulated in the ChtmlView class. We use the latter one. It should be noted that we need to get its interface after generating the IHTMLDocument object in ChtmlView. void CSkhttpView :: OnInitialUpdate () {Navigate2 ( "about: blank");} void CSkhttpView :: OnDocumentComplete (LPCTSTR lpszURL) {LPDISPATCH lpdisp; HRESULT hr; lpdisp = GetHtmlDocument (); hr = lpdisp-> QueryInterface (IID_IHTMLDocument2, ( Void **) & phmdoc2); void CSkhttpView :: OnReceiveMessage (WPARAM wParam, LPARAM 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 rt = m_socket.send (BUF , lstrlen (buf), 0); if (IRET == Socket_ERROR) MessageBox ("Socket Send Error", NULL, MB_OK;} We send an HTTP protocol to the server "Get http://www.163.net http / 1.1/r/n/r/N ", it retrieves the page of the specified URI address. The server responded to we accept it via Socket. IHTMLDocument2 contains many object excuses, through many PUT_, Get_Methals We can get these objects, events, methods.

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.053, SQL: 9