📄 0,1410,22288,00.html
字号:
<!-- 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> The hardest thing about creating your own socket
connection for use with the web is simply getting started. From the
help files, one can quickly learn how to connect a TClientSocket (from
the Internet group) to a given server. But once you've achieved a
connection, what do you do with it?
<p> After connecting to your server, you must create
a TWinSocketStream. The TWinSocketStream is the stream that you will
use to actually read and write to the server with. 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> Using the Read and Write methods, you can get all
of the information you need. The only problem with a blocking connection,
is that there is no way to tell when the server is done sending information.
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> To begin reading from a server, a command of this
form must be sent:
<br> GET path/file.html nrnr
<br> This command, sent with your TWinSocketStream's
Write method, tells the server to being sending the file to you.
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 <Classes.hpp></font></tt>
<br><tt><font color="#009900">#include <Controls.hpp></font></tt>
<br><tt><font color="#009900">#include <StdCtrls.hpp></font></tt>
<br><tt><font color="#009900">#include <Forms.hpp></font></tt>
<br><tt><font color="#009900">#include <ScktComp.hpp></font></tt>
<br><tt><font color="#009900">#include <ExtCtrls.hpp></font></tt>
<br><tt><font color="#009900">#include <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> TClientSocket *ClientSocket1;</tt>
<br><tt> TPanel *Panel1;</tt>
<br><tt> TEdit *Edit1;</tt>
<br><tt> TLabel *Label1;</tt>
<br><tt> TPanel *Panel2;</tt>
<br><tt> TButton *Button1;</tt>
<br><tt> TButton *Button2;</tt>
<br><tt> TLabel *Label2;</tt>
<br><tt> TLabel *lblURL;</tt>
<br><tt> TLabel *Label3;</tt>
<br><tt> TLabel *lblStatus;</tt>
<br><tt> TButton *Button3;</tt>
<br><tt> TLabel *Label4;</tt>
<br><tt> TLabel *Label5;</tt>
<br><tt> TLabel *lblCount;</tt>
<br><tt> TRichEdit *Memo1;</tt>
<br><tt> TEdit *edtMax;</tt>
<br><tt> <b>void __fastcall</b> ClientSocket1Connect(TObject
*Sender,</tt>
<br><tt> TCustomWinSocket
*Socket);</tt>
<br><tt><b> void __fastcall</b> ClientSocket1Connecting(TObject
*Sender,</tt>
<br><tt> TCustomWinSocket
*Socket);</tt>
<br><tt><b> void __fastcall </b>Button1Click(TObject *Sender);</tt>
<br><tt><b> void __fastcall</b> Button2Click(TObject *Sender);</tt>
<br><tt><b> void __fastcall</b> ClientSocket1Disconnect(TObject
*Sender,</tt>
<br><tt> TCustomWinSocket
*Socket);</tt>
<br><tt> <b> void __fastcall</b> Button3Click(TObject *Sender);</tt>
<br><tt><b>private</b>:</tt>
<br><tt> AnsiString URL; <i><font color="#000099">// User declarations</font></i></tt>
<br><tt><b>public</b>: <i><font color="#000099">// User declarations</font></i></tt>
<br><tt><b> __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>
<p><i><tt><font color="#000099">//---------------------------------------------------------------------------</font></tt></i>
<p><tt><font color="#009900">#include <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> : 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> TCustomWinSocket *Socket)</tt>
<br><tt>{</tt>
<br><tt> Label1->Caption=<font color="#3333FF">"HTML Grab:
Connected"</font>;</tt>
<br><tt> lblCount->Caption=(0);</tt>
<br><tt> AnsiString Directory(ExtractURLPath(Edit1->Text));</tt>
<p><tt> TWinSocketStream* wnscktstrmMain = new TWinSocketStream(Socket,
10000);</tt>
<p><tt> AnsiString buf=<font color="#3333FF">"Type in a valid
URL and hit connect..."</font>;</tt>
<p><tt><b> if</b>(Memo1->Lines->Strings[0]==buf)</tt>
<br><tt> Memo1->Lines->Delete(0);</tt>
<br><tt>
<i><font color="#000099">//HTML commands must be made in the form of "GET
foo/index.html nrnr"</font></i></tt>
<br><tt> buf="GET "+Directory; <i><font color="#000099">//GET
command for html transfer</font></i></tt>
<br><tt> buf+=(" nrnr"); <i><font color="#000099">//required
as end transfer tag</font></i></tt>
<br><tt> <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> <b>int</b> i=1;</tt>
<br><tt> <b>char</b> point[LENGTH]={<font color="#3333FF">"o"</font>};</tt>
<br><tt> <b>if</b>(wnscktstrmMain->Read(point,LENGTH)){</tt>
<br><tt> point[LENGTH]='0';</tt>
<br><tt> lblStatus->Caption=<font color="#3333FF">"Success"</font>;</tt>
<br><tt> Memo1->Lines->Add("");</tt>
<br><tt> Memo1->Lines->Add("Source
code from "+Edit1->Text);</tt>
<br><tt> Memo1->Lines->Add("");</tt>
<br><tt> Memo1->Lines->Add(<font color="#3333FF">"-------------------------------------"</font>);</tt>
<br><tt> buf=point;</tt>
<br><tt> Memo1->Text=Memo1->Text+buf;</tt>
<br><tt> lblCount->Caption=(lblCount->Caption.ToInt()+1);
<i><font color="#000099">//incrementing
line counter</font></i></tt>
<br><tt> <b>int</b> j=0;</tt>
<br><tt> while(i>0&&i<(edtMax->Text.ToInt())){</tt>
<br><tt>
lblCount->Caption=(lblCount->Caption.ToInt()+1); <i><font color="#000099">//incrementing
line counter</font></i></tt>
<br><tt>
wnscktstrmMain->Read(point,LENGTH);
<i><font color="#000099">//reading from stream</font></i></tt>
<br><tt>
buf=point;</tt>
<br><tt> <b>if</b>(buf.AnsiPos(<font color="#3333FF">"</html>"</font>)){<i><font color="#000099">//testing
for end html tag</font></i></tt>
<br><tt>
j=buf.AnsiPos(<font color="#3333FF">"</html>"</font>);</tt>
<br><tt>
Memo1->Text=Memo1->Text+buf.SubString(1,j+6);</tt>
<br><tt>
i=0;
<i><font color="#000099">//end read</font></i></tt>
<br><tt>
}</tt>
<br><tt> <b>if</b>(buf.AnsiPos("</HTML>")){<i><font color="#000099">//testing
for end html tag</font></i></tt>
<br><tt>
j=buf.AnsiPos(<font color="#3333FF">"</HTML>"</font>);</tt>
<br><tt>
Memo1->Text=Memo1->Text+buf.SubString(1,j+6);</tt>
<br><tt>
i=0;
<i><font color="#000099">//end read</font></i></tt>
<br><tt>
}</tt>
<br><tt>
int p=0;</tt>
<br><tt>
while(i!=0&&p>=0&&p<10){ <i><font color="#000099">//p
is the # of lines to be</font></i></tt>
<br><i><tt><font color="#000099">
//checked from the bottom of the page for </html> tag</font></tt></i>
<br><tt>
<b>if</b>(Memo1->Lines->Strings[(Memo1->Lines->Count)-p].AnsiPos(<font color="#3333FF">"</html>"</font>)){</tt>
<br><tt>
<i><font color="#000099">//checking for truncated /html tag</font></i></tt>
<br><tt>
j=Memo1->Lines->Strings[(Memo1->Lines->Count)-p].AnsiPos(<font color="#3333FF">"</html>"</font>);</tt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -