Tutorials to .com

Tutorials to .com » Software » Vb » Use Socket and the MSHTML Object Model to create their own browser

Use Socket and the MSHTML Object Model to create their own browser

Print View , by: iSee ,Total views: 26 ,Word Count: 1129 ,Date: Wed, 26 Aug 2009 Time: 10:18 AM


Of the Central University of Finance and Management Information Department of Xue Ying

HTTP protocol and the birth of WEB browser to our network of adding more exciting. However, in practical applications, we may have different needs, rather than simply use the browser, such as in our application by adding functionality to browse the Internet. Microsoft ChtmlView class implements a convenient web browser. But it is very flexible to allow users to dynamically change the page element you want. This article discusses some ways to use Socket transport Html documents, using Microsoft's dynamic MSHTML Object Model of the internal mechanisms of some browsers.
We all know that HTML is a markup language document composition, commonly known as Tag. Microsoft's browser, IE implements these markers-one correspondence of the object model (objectmodel), by MSHTML.DLL package. IE browser implementation is achieved by MSHTML.DLL. By MSHTML.DLL object model we can directly operate the properties and methods. MSHTML object model is based on COM component object, the object's interface is based on Idispatch, operating MSHTML object model must pass Idispatch interface. MSHTML in encapsulates many of these interfaces, for example, IhtmlAnchorElement interface corresponds to HTML document with hyperlinks mark <A>, IHtmlHRElement interface corresponds to <HR> mark, IhtmlTable interface corresponds to <table> tag. One of the most important thing is IhtmlDocument2 interface, which corresponds to Document component. Document components of both the equivalent of HTML. javascript used by people who should be more familiar with it.
Let us give some examples to explain the application of MSHTML. In the example, first, before I talk about Socket and HTTP protocols. TCP connect to a server via HTTP protocol and the Client, it works on port 80. Between the Client and the Server through the HTTP request / response mechanism to communicate. HTTP messages are divided into Request and Response. Message from the start of each line, the message headers and message body composition. Form are as follows:
generic-message = start-line
* message-header
CRLF
[Message-body]

start-line of the form are as follows:
start-line = Request-Line | Status-Line
Request-Line for the Client to the Server to issue the request form are as follows:
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Method, including GET, POST and so on, in this case, we only use a simple GET request to the server.
HTTP protocol in detail please refer to RFC2068.
In the vc + + to create a new EXE project single-document files, in order to use MSHTML our view class inherits from the ChtmlView. ChtmlView class encapsulates the MSHTML interface. To the project by adding Socket class.
class CHttpSocket: public CSocket
(
... ... ... ... ... ...

protected:
CWnd * m_pParentWnd;
)
Which m_pParentWnd point to our view class, used to deliver the message. In the view class is defined Socket.
class CSkhttpView: public CHtmlView
(
protected: / / create from serialization only
CSkhttpView ();
DECLARE_DYNCREATE (CSkhttpView)
... ... ... ... ....
protected:
CHttpSocket m_socket;
IHTMLDocument2 * phmDoc2;
... ... ..
)
phmDoc2 for IHTMLDocument2 interface. Initialization Socke, we want to log on to connect the site, assumed to be 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);
)
Here we have made IhtmlDocument2 interface. access to this interface are generally two ways. One is to use CoCreateInstance, then call QueryInterface. Another is to use the MSHTML control object get_Document, in the ChtmlView class encapsulates this interface. We use the latter. Note that, we need to ChtmlView build IhtmlDocument object to get its interface.
void CSkhttpView:: OnInitialUpdate ()
(
Navigate2 ( "about: blank");
)
void CSkhttpView:: OnDocumentComplete (LPCTSTR lpszURL)
(
LPDISPATCH lpdisp;
HRESULT hr;
lpdisp = GetHtmlDocument ();
hr = lpdisp-> QueryInterface (IID_IHTMLDocument2, (void **) & phmDoc2);
)
The ChtmlView initialization call Navigate2 () create an empty document, the document after the election call GetHtmlDocument get a Idispatch () interface, call QueryInterface to obtain IHTMLDocument2 interface.
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 iRet = m_socket.Send (buf, lstrlen (buf), 0);
if (iRet == SOCKET_ERROR)
MessageBox ( "socket send error", NULL, MB_OK);
)
We send the HTTP protocol to the server the request "GET http://www.163.net HTTP/1.1 \ r \ n \ r \ n", it is to retrieve the specified URI address of the web page. Issued a response to our server through Socket accept. IhtmlDocument2 contains many objects an excuse by many put_, get_ way we can get the properties of these objects, events and methods. In this case we get body object, which corresponds to the HTML text block in the <body> </ body> object. By IhtmlDocument2 of get_body () to be the designated body object IHTMLElement interface, call IHTMLElement interface put_innerHTML () will receive the content into the Document in. At this point in our view, it will display the page.
This is just a brief introduction to the look MSHTML object which actually contain a lot of interfaces and functions, through any of these interfaces that we can design our own style browser. The comrades who are interested can refer to MSDN documentation.


Visual Basic Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.