Delphi取得网页内容的另一种办法
来源:互联网 发布:大学生笔记本必备软件 编辑:程序博客网 时间:2024/05/22 05:19
初步了解了一下HTTP1.1 小试了一下,利用TcpClient控件发送HTTP1.1命令到IIS后取得网页内容。
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Sockets;
type
TForm1 = class(TForm)
Button1: TButton;
TcpClient1: TTcpClient;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Test: string;
HttpLen: integer;
begin
TcpClient1.RemoteHost := 'www.163.cn';
TcpClient1.RemotePort := '80';
TcpClient1.Active := true;
if TcpClient1.Connected then
begin
//发送HTTP1.1指令
TcpClient1.Sendln('GET / HTTP/1.1');
TcpClient1.Sendln('Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*');
TcpClient1.Sendln('User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon)');
TcpClient1.Sendln('Host: www.163.cn');
TcpClient1.Sendln('Connection: Keep-Alive');
TcpClient1.Sendln('');
end;
HttpLen := 0;
//循环读取所有返回的数据头信息
while true do
begin
Test := TcpClient1.Receiveln();
Memo1.Lines.Add(Test);
if Test = '' then Break;
if Pos('Content-Length: ', Test) > 0 then
begin
Delete(Test, 1, 16);
HttpLen := StrToInt(Test); //获取将要读取的数据长度
end;
end;
LockWindowUpdate(Memo1.Handle);
Memo1.Clear;
//循环读取所有返回的数据直接数据接收完毕
while (Length(Memo1.Text) < HttpLen - 2) or (TcpClient1.WaitForData(0) and (HttpLen = 0)) do
begin
Memo1.Lines.Add(TcpClient1.Receiveln(#$0D#$0A));
Application.ProcessMessages;
Edit1.Text := Format('总长度:%d 已下载:%d ', [HttpLen, Length(Memo1.Text)]);
end;
//有些网页返回的非ANSI字符串,则需要转码,否则中文全是乱码
if Length(Memo1.Text) = HttpLen - 2 then Memo1.Text := Utf8ToAnsi(Memo1.Text);
LockWindowUpdate(0);
Memo1.Lines.SaveToFile('d:/test.txt');
ShowMessage('完成数据下载');
end;
end.
这只是小试了一下HTTP1.1的几个指令,通过多线程等,可以让这个程序更完美一点,以后再搞。并且这里也只是试了一下GET方式的指令,有空再试试POST方法向IIS提交数据等功能。
- Delphi取得网页内容的另一种办法
- delphi 使用webbrowser取得网页中表格(Table)内容的正确方法
- delphi使用webbrowser取得网页中无序列表(ul)内容的正确方法
- 使用HttpWebRequest的POST取得网页内容
- 使用HttpWebRequest的POST取得网页内容
- 取得WebBrowser网页内容
- 取得指定网页内容
- setToolTipText弹出网页内容的办法
- delphi 抓取网页内容的程序
- delphi抓取网页内容的程序
- delphi 入门之《获取网页的内容》
- 使用HttpWebRequest的POST取得网页内容(异步操作)
- 用刷新网页的办法更新user control控件内容
- vxWorks应用程序加载的另一种办法
- android 图片点击变暗的另一种办法
- vxWorks应用程序加载的另一种办法
- Delphi的问题解决办法:
- 用XMLHTTP方式取得网页内容
- 一个人关于LINK : fatal error LNK1104: cannot open file "nafxcwd.lib"的解决
- 老宋开博喽!
- 早该如此了~~国家文物局要求认真审核佳士得申报进出境文物
- 如果 真的 有 下 辈子.
- 韩国房价1亿韩元1平
- Delphi取得网页内容的另一种办法
- 圆明园12生肖兽首图 (10张) -- 中国人都该看看
- 就十五分钟,学一回毛主席,不行吗
- JMS和MDB总结
- c#的串口控件操作
- 北电中国高管自述破产40天惊魂:对手抓着猛打
- 《大话设计模式之依赖倒转原则》
- 获取递增的列的值
- phper避免的事项