⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,22288,00.html

📁 C++builder学习资料C++builder
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!-- Vignette StoryServer 4 Wed Nov 22 20:44:58 2000 -->

 <!-- Vignette StoryServer 4 Fri Aug 10 21:44:32 2001 -->



   



       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>



<HEAD>



<TITLE>Using a blocking TClientSocket Connection for HTML Transfer</TITLE>

</HEAD>

<BODY MARGINWIDTH="5" MARGINHEIGHT="5" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF">



<A NAME="top"></A>

<table>

        



<TR>

 

   <td>

<SPAN CLASS="title3"><b>Using a blocking TClientSocket Connection for HTML Transfer</b></SPAN> 

		 

	<BR> 

 

<P> 

<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>This TI explains how to use a blocking TClientSocket  in conjunction with a TwinSocketStream to send HTML commands to a server, in order to recieve files.  Explains the actual commands as well.</BLOCKQUOTE><P> 

  

<font size=+1>Using a blocking TClientSocket Connection for HTML 

Transfer</font> 

<p>&nbsp;&nbsp;&nbsp; The hardest thing about creating your own socket 

connection for use with the web is simply getting started.&nbsp; From the 

help files, one can quickly learn how to connect a TClientSocket (from 

the Internet group) to a given server.&nbsp; But once you've achieved a 

connection, what do you do with it? 

<p>&nbsp;&nbsp;&nbsp; After connecting to your server, you must create 

a TWinSocketStream.&nbsp; The TWinSocketStream is the stream that you will 

use to actually read and write to the server with.&nbsp; Once the TWinSocketStream 

is in place, you can begin making your commands to the server to retrieve 

the html that you want to process. 

<p>&nbsp;&nbsp;&nbsp; Using the Read and Write methods, you can get all 

of the information you need.&nbsp; The only problem with a blocking connection, 

is that there is no way to tell when the server is done sending information.&nbsp; 

This can be handled by only taking a certain number of lines at a time, 

or by parsing the text for an end html tag. 

<p>&nbsp;&nbsp;&nbsp; To begin reading from a server, a command of this 

form must be sent: 

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GET path/file.html nrnr 

<br>&nbsp;&nbsp;&nbsp; This command, sent with your TWinSocketStream's 

Write method, tells the server to being sending the file to you.&nbsp; 

It is your job to know when the file is complete. 

<p><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><i><tt><font color="#000099">//Begin MainForm.h</font></tt></i> 

<br><tt><font color="#009900">#ifndef MainFormH</font></tt> 

<br><tt><font color="#009900">#define MainFormH</font></tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><font color="#009900">#include &lt;Classes.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;Controls.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;StdCtrls.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;Forms.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;ScktComp.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;ExtCtrls.hpp></font></tt> 

<br><tt><font color="#009900">#include &lt;ComCtrls.hpp></font></tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><b>class</b> TForm1 : <b>public</b> TForm</tt> 

<br><tt>{</tt> 

<br><tt><b>__published:</b> <i><font color="#000099">// IDE-managed Components</font></i></tt> 

<br><tt>&nbsp;&nbsp; TClientSocket *ClientSocket1;</tt> 

<br><tt>&nbsp;&nbsp; TPanel *Panel1;</tt> 

<br><tt>&nbsp;&nbsp; TEdit *Edit1;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *Label1;</tt> 

<br><tt>&nbsp;&nbsp; TPanel *Panel2;</tt> 

<br><tt>&nbsp;&nbsp; TButton *Button1;</tt> 

<br><tt>&nbsp;&nbsp; TButton *Button2;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *Label2;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *lblURL;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *Label3;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *lblStatus;</tt> 

<br><tt>&nbsp;&nbsp; TButton *Button3;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *Label4;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *Label5;</tt> 

<br><tt>&nbsp;&nbsp; TLabel *lblCount;</tt> 

<br><tt>&nbsp;&nbsp; TRichEdit *Memo1;</tt> 

<br><tt>&nbsp;&nbsp; TEdit *edtMax;</tt> 

<br><tt>&nbsp;&nbsp; <b>void __fastcall</b> ClientSocket1Connect(TObject 

*Sender,</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCustomWinSocket 

*Socket);</tt> 

<br><tt><b>&nbsp;&nbsp; void __fastcall</b> ClientSocket1Connecting(TObject 

*Sender,</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCustomWinSocket 

*Socket);</tt> 

<br><tt><b>&nbsp;&nbsp; void __fastcall </b>Button1Click(TObject *Sender);</tt> 

<br><tt><b>&nbsp;&nbsp; void __fastcall</b> Button2Click(TObject *Sender);</tt> 

<br><tt><b>&nbsp;&nbsp; void __fastcall</b> ClientSocket1Disconnect(TObject 

*Sender,</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCustomWinSocket 

*Socket);</tt> 

<br><tt>&nbsp;&nbsp;<b> void __fastcall</b> Button3Click(TObject *Sender);</tt> 

<br><tt><b>private</b>:</tt> 

<br><tt>&nbsp;&nbsp; AnsiString URL; <i><font color="#000099">// User declarations</font></i></tt> 

<br><tt><b>public</b>:&nbsp; <i><font color="#000099">// User declarations</font></i></tt> 

<br><tt><b>&nbsp;&nbsp; __fastcall</b> TForm1(TComponent* Owner);</tt> 

<br><tt>};</tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><b>extern</b> PACKAGE TForm1 *Form1;</tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><font color="#009900">#endif</font></tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<p><i><tt><font color="#000099">//End MainForm.h</font></tt></i> 

<p><i><tt><font color="#000099">//Begin MainForm.cpp</font></tt></i> 

<br>&nbsp; 

<p><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<p><tt><font color="#009900">#include &lt;vcl.h></font></tt> 

<br><tt><font color="#009900">#pragma hdrstop</font></tt> 

<p><tt><font color="#009900">#include "URLUtil.h"</font></tt> 

<br><tt><font color="#009900">#include "MainForm.h"</font></tt> 

<p><tt><font color="#009900">#define LENGTH 80</font></tt> 

<p><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><font color="#009900">#pragma package(smart_init)</font></tt> 

<br><tt><font color="#009900">#pragma resource "*.dfm"</font></tt> 

<br><tt>TForm1 *Form1;</tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<br><tt><b>__fastcall</b> TForm1::TForm1(TComponent* Owner)</tt> 

<br><tt>&nbsp;&nbsp; : TForm(Owner)</tt> 

<br><tt>{</tt> 

<br><tt>}</tt> 

<br><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i> 

<p><tt><b>void __fastcall</b> TForm1::ClientSocket1Connect(TObject *Sender,</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCustomWinSocket *Socket)</tt> 

<br><tt>{</tt> 

<br><tt>&nbsp;&nbsp; Label1->Caption=<font color="#3333FF">"HTML Grab: 

Connected"</font>;</tt> 

<br><tt>&nbsp;&nbsp; lblCount->Caption=(0);</tt> 

<br><tt>&nbsp;&nbsp; AnsiString Directory(ExtractURLPath(Edit1->Text));</tt> 

<p><tt>&nbsp;&nbsp; TWinSocketStream* wnscktstrmMain = new TWinSocketStream(Socket, 

10000);</tt> 

<p><tt>&nbsp;&nbsp; AnsiString buf=<font color="#3333FF">"Type in a valid 

URL and hit connect..."</font>;</tt> 

<p><tt><b>&nbsp;&nbsp; if</b>(Memo1->Lines->Strings&#91;0&#93;==buf)</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Delete(0);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<i><font color="#000099">//HTML commands must be made in the form of "GET 

foo/index.html nrnr"</font></i></tt> 

<br><tt>&nbsp;&nbsp; buf="GET "+Directory;&nbsp;&nbsp;&nbsp; <i><font color="#000099">//GET 

command for html transfer</font></i></tt> 

<br><tt>&nbsp;&nbsp; buf+=(" nrnr");&nbsp;&nbsp;&nbsp;&nbsp; <i><font color="#000099">//required 

as end transfer tag</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp; <b>if</b>(wnscktstrmMain->Write(buf.c_str(),buf.Length())){ 

<i><font color="#000099">//if 

write succeeded, read from stream</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>int</b> i=1;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>char</b> point&#91;LENGTH&#93;={<font color="#3333FF">"o"</font>};</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>if</b>(wnscktstrmMain->Read(point,LENGTH)){</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; point&#91;LENGTH&#93;='0';</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lblStatus->Caption=<font color="#3333FF">"Success"</font>;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add("");</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add("Source 

code from "+Edit1->Text);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add("");</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add(<font color="#3333FF">"-------------------------------------"</font>);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buf=point;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Text=Memo1->Text+buf;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lblCount->Caption=(lblCount->Caption.ToInt()+1); 

<i><font color="#000099">//incrementing 

line counter</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>int</b> j=0;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(i>0&amp;&amp;i&lt;(edtMax->Text.ToInt())){</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

lblCount->Caption=(lblCount->Caption.ToInt()+1); <i><font color="#000099">//incrementing 

line counter</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

wnscktstrmMain->Read(point,LENGTH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

 

<i><font color="#000099">//reading from stream</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

buf=point;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>if</b>(buf.AnsiPos(<font color="#3333FF">"&lt;/html>"</font>)){<i><font color="#000099">//testing 

for end html tag</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

j=buf.AnsiPos(<font color="#3333FF">"&lt;/html>"</font>);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

Memo1->Text=Memo1->Text+buf.SubString(1,j+6);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

i=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

 

<i><font color="#000099">//end read</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

}</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>if</b>(buf.AnsiPos("&lt;/HTML>")){<i><font color="#000099">//testing 

for end html tag</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

j=buf.AnsiPos(<font color="#3333FF">"&lt;/HTML>"</font>);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

Memo1->Text=Memo1->Text+buf.SubString(1,j+6);</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

i=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

 

<i><font color="#000099">//end read</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

}</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

int p=0;</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

while(i!=0&amp;&amp;p>=0&amp;&amp;p&lt;10){&nbsp;&nbsp;&nbsp; <i><font color="#000099">//p 

is the # of lines to be</font></i></tt> 

<br><i><tt><font color="#000099">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

//checked from the bottom of the page for &lt;/html> tag</font></tt></i> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<b>if</b>(Memo1->Lines->Strings&#91;(Memo1->Lines->Count)-p&#93;.AnsiPos(<font color="#3333FF">"&lt;/html>"</font>)){</tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<i><font color="#000099">//checking for truncated /html tag</font></i></tt> 

<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

j=Memo1->Lines->Strings&#91;(Memo1->Lines->Count)-p&#93;.AnsiPos(<font color="#3333FF">"&lt;/html>"</font>);</tt> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -