unit httpgetThread;interface
uses
Classes,IdHTTP,winInet,Windows,Forms;type clssThread_httpget = class(TThread) private
idhttp1:tidhttp;
html_str :string;
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(Suspended:boolean;fidhttp:tidhttp);
procedure setstr(htmlstr:string);
function getstr:string;
end;implementationprocedure clssThread_httpget.setstr(htmlstr:string);
var i:integer;
begin
self.html_str:=htmlstr; //这个地方执行后,html_str值一直没有变化。
end;function clssThread_httpget.getstr:string ;
begin
result:=html_str;
end;constructor clssThread_httpget.Create(Suspended:Boolean;fidhttp:tidhttp);
begin
inherited Create(Suspended);
idhttp1:=fidhttp;
html_str:='init....';
FreeOnTerminate:=True;
end;
function CanGetIECookie(const URL: string; var Cookie: string): boolean;
var
lpvBuffer: array[0..1000] of byte;
lpdwBufferLength: cardinal;
begin
lpdwBufferLength := sizeof(lpvBuffer);
result := InternetGetCookie(PChar(URL), nil, @lpvBuffer, lpdwBufferLength);
if result then
Cookie := pchar(@lpvBuffer);
end;function httpget(idhttp1:tidhttp):string;
var url,str,cookiestr,temp:string;
begin
url:= 'http://www.e0575.cn/dispuser.asp?id=71002';
idhttp1.Request.Host:='www.e0575.cn';
idhttp1.Request.AcceptLanguage:='zh-cn';
idhttp1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322)';
idhttp1.Request.ProxyConnection:='Keep-Alive' ;
idhttp1.Request.ContentType:='text/html';
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
idhttp1.AllowCookies:=true;
if CanGetIECookie(url,str) then
begin
CookieStr := 'Cookie: ' + str;
idHttp1.Request.CustomHeaders.Text := CookieStr;
end;
try
temp:=idhttp1.Get(url);
except
application.MessageBox('correct happened!','wrong',MB_iconerror+MB_OK);
application.Terminate;
end; result:=temp;
idhttp1.Free;end;procedure clssThread_httpget.Execute();
VAR temp:string;
begin
While not Terminated do
begin
temp:= httpget(idhttp1);
//Synchronize(httpget(idhttp1));
setstr(temp);
end;
end;end.
uses
Classes,IdHTTP,winInet,Windows,Forms;type clssThread_httpget = class(TThread) private
idhttp1:tidhttp;
html_str :string;
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(Suspended:boolean;fidhttp:tidhttp);
procedure setstr(htmlstr:string);
function getstr:string;
end;implementationprocedure clssThread_httpget.setstr(htmlstr:string);
var i:integer;
begin
self.html_str:=htmlstr; //这个地方执行后,html_str值一直没有变化。
end;function clssThread_httpget.getstr:string ;
begin
result:=html_str;
end;constructor clssThread_httpget.Create(Suspended:Boolean;fidhttp:tidhttp);
begin
inherited Create(Suspended);
idhttp1:=fidhttp;
html_str:='init....';
FreeOnTerminate:=True;
end;
function CanGetIECookie(const URL: string; var Cookie: string): boolean;
var
lpvBuffer: array[0..1000] of byte;
lpdwBufferLength: cardinal;
begin
lpdwBufferLength := sizeof(lpvBuffer);
result := InternetGetCookie(PChar(URL), nil, @lpvBuffer, lpdwBufferLength);
if result then
Cookie := pchar(@lpvBuffer);
end;function httpget(idhttp1:tidhttp):string;
var url,str,cookiestr,temp:string;
begin
url:= 'http://www.e0575.cn/dispuser.asp?id=71002';
idhttp1.Request.Host:='www.e0575.cn';
idhttp1.Request.AcceptLanguage:='zh-cn';
idhttp1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322)';
idhttp1.Request.ProxyConnection:='Keep-Alive' ;
idhttp1.Request.ContentType:='text/html';
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
idhttp1.AllowCookies:=true;
if CanGetIECookie(url,str) then
begin
CookieStr := 'Cookie: ' + str;
idHttp1.Request.CustomHeaders.Text := CookieStr;
end;
try
temp:=idhttp1.Get(url);
except
application.MessageBox('correct happened!','wrong',MB_iconerror+MB_OK);
application.Terminate;
end; result:=temp;
idhttp1.Free;end;procedure clssThread_httpget.Execute();
VAR temp:string;
begin
While not Terminated do
begin
temp:= httpget(idhttp1);
//Synchronize(httpget(idhttp1));
setstr(temp);
end;
end;end.
解决方案 »
- datasource 与 dataset
- webBrowser问题,解决给100分
- delphi 开发界面
- delphi7.0中f1book的使用?
- 获得edit句柄
- 在treeview中构造图片
- Delphi如何做到监视一个WEB窗口的运行,并能截取WEB窗口发往网站的消息?在线等候...
- 关于StringGird?
- 求助:使用TQuery而不是TTable来读取Blob字段。
- 如何在整个硬盘中搜索一个已知文件名的文件,返回路径??
- 老问题“无法为更新定位行。一些值可能已在最后一次读取后已更改。”
- Express Printing system v3.3.3 汉化版的问题‘invalid pointer operation’
用mythread1.getstr不能返回正确的html_str属性值.也就是说,我在类定义里边给html_str属性付值并没有带到实例中,而在unit2中用mythread1.setstr('ssss')可以达到修改html_str属性值得目的,而在类里边的过程clssThread_httpget.Execute中不能修改该值。我该怎么办?
或直接更改 html_str 的值就可以了~html_str:=temp;
你试一下这样吧
MYTHREAD1:=clssThread_httpget.CREATE(TRUE,IDHTTP1); 把你的Fasle改为True,作用在于一开始就把你的马线程挂起
然后在后面加上
Mythread1.resume;把线程唤起即可。或者把你
constructor clssThread_httpget.Create(Suspended:Boolean;fidhttp:tidhttp);
begin
inherited Create(Suspended);
idhttp1:=fidhttp;
html_str:='init....';
FreeOnTerminate:=True;
end;
改为
constructor clssThread_httpget.Create(Suspended:Boolean;fidhttp:tidhttp);
begin
idhttp1:=fidhttp;
html_str:='init....';
inherited Create(Suspended);
FreeOnTerminate:=True;
end;
自定义一个多线程类,里边的execute过程是重载的。我在外部单元实例化后,如果参数为Suspended=false,那么他就执行execute过程,是吗?
你说:“我想在Execute中是已修改了该值的,但你的Create却把这个值改回来已而。”
那create应该首先执行,然后是execute过程,怎么回改回来呢?
1.
function httpget(idhttp1:tidhttp):string;
var url,str,cookiestr,temp:string;
中不要去调用这些东西:
application.MessageBox('correct happened!','wrong',MB_iconerror+MB_OK);
application.Terminate;
一般我们是写个日志的;2. 如果你要把提示信息传递到完面来,也就是你要在线程外调用getstr函数,会出现问题的;因为string是自动分配内存的,不是同一个线程操作这个string会产生异常的(概率不是很高,但影响系统的稳定性);如果你的提示字符串不是很长的话,把html_str :string;改为html_str: shortstring;这样就没有问题了