急救,懂idhttp的进来帮忙(倾家荡产) ( 积分: 192 )

  • 主题发起人 主题发起人 qingtz
  • 开始时间 开始时间
Q

qingtz

Unregistered / Unconfirmed
GUEST, unregistred user!
我用idhttp访问一个网站的时候出错了,不知道是什么原因,大虾帮忙!
下面是idhttp请求
idhttp.AllowCookies:=true;
idhttp.CookieManager:=form1.Cookman;
idhttp.HandleRedirects:=true;
idhttp.ProxyParams.ProxyServer:='*.*.*.*';
idhttp.ProxyParams.ProxyPort:=80;
idhttp.ProtocolVersion:=pv1_0;
idhttp.Request.Accept:='image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-gsarcade-launch, */*';
idhttp.Request.AcceptLanguage:='zh-cn';
idhttp.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)';
然后运行程序用截获软件截获发出的数据是
GET http://******.com:80/user/f_klick.php?step=1&id=12219 name= HTTP/1.0
Content-Type: text/html
Proxy-Connection: keep-alive
Host: losegroup.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-gsarcade-launch, */*
Accept-Language: zh-cn
Referer: http://******.com/user/f_klick.php?id=12219
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)
Cookie: pw=******; id=******
收到的数据是
HTTP/1.1 406 Not Acceptable
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html
Proxy-Connection: close
Connection: close
Content-Length: 616

<HTML><HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<FONT face=&quot;Helvetica&quot;>
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width=&quot;80%&quot;>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>
<big>Request Error (unsupported_protocol)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>
Your request used a protocol that is not currently supported.
</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>

</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot; SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

为什么 会出现错误呢?我用浏览器浏览的时候发出去的数据数和我用idhttp的一样,为什么收到的就不同了呢?我明明发出的是http1.0协议,为什么收到的是http1.1呢?可是用浏览器收到的却是http1.0的协议?
 
我用idhttp访问一个网站的时候出错了,不知道是什么原因,大虾帮忙!
下面是idhttp请求
idhttp.AllowCookies:=true;
idhttp.CookieManager:=form1.Cookman;
idhttp.HandleRedirects:=true;
idhttp.ProxyParams.ProxyServer:='*.*.*.*';
idhttp.ProxyParams.ProxyPort:=80;
idhttp.ProtocolVersion:=pv1_0;
idhttp.Request.Accept:='image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-gsarcade-launch, */*';
idhttp.Request.AcceptLanguage:='zh-cn';
idhttp.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)';
然后运行程序用截获软件截获发出的数据是
GET http://******.com:80/user/f_klick.php?step=1&amp;id=12219 name= HTTP/1.0
Content-Type: text/html
Proxy-Connection: keep-alive
Host: losegroup.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-gsarcade-launch, */*
Accept-Language: zh-cn
Referer: http://******.com/user/f_klick.php?id=12219
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)
Cookie: pw=******; id=******
收到的数据是
HTTP/1.1 406 Not Acceptable
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html
Proxy-Connection: close
Connection: close
Content-Length: 616

<HTML><HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<FONT face=&quot;Helvetica&quot;>
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width=&quot;80%&quot;>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>
<big>Request Error (unsupported_protocol)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>
Your request used a protocol that is not currently supported.
</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot;>

</FONT>
</TD></TR>
<TR><TD>
<FONT face=&quot;Helvetica&quot; SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

为什么 会出现错误呢?我用浏览器浏览的时候发出去的数据数和我用idhttp的一样,为什么收到的就不同了呢?我明明发出的是http1.0协议,为什么收到的是http1.1呢?可是用浏览器收到的却是http1.0的协议?
 
帮忙呀,救命那……
 
请贴出你的释放方法
 
constructor TEvent.Create(EventAttributes: PSecurityAttributes; ManualReset,
InitialState: Boolean; const Name: string);
第二个参数ManualReset应该是手动复位
 
问题还是没有解决,改成下面样子也有问题,运行时有时候可以,有时候又不行!释放的方法是线程运行完毕后自动释放,请各位再帮忙看看!unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TWorkThread = class(TThread)
private
n:integer;
protected
procedure Execute; override;
public
constructor Create(m:integer);
end;

type
TManagerThread = class(TThread)
private
Work:array[0..4] of TWorkThread;
j:integer;
protected
procedure Execute;override;
public
constructor Create(i:integer);
end;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
manager:array[0..1] of TManagerThread;
public
{ Public declarations }
end;

var
Form1: TForm1;
FEvent: array[0..1] of THandle;
implementation

{$R *.dfm}
{ --------------- TWorkThread----------------------------------}
constructor TWorkThread.Create(m:integer);
begin
n:=m;
FreeOnTerminate := True;
inherited create(false);
end;

procedure TWorkThread.Execute;
var
l:integer;
begin
l:=random(10);
sleep(l*1000);
setevent(FEvent[n]); //问题可能在这里,是不是两个子线程的setevent时间间隔太短,而下面waitforsingleobject还没有处理完毕?
end;

{ --------------- TManagerThread----------------------------------}

constructor TManagerThread.Create(i:integer);
begin
j:=i;
FreeOnTerminate := True;
inherited create(false);
end;

procedure TManagerThread.Execute;
var
i,k:integer;
Code: Cardinal ;
begin
k:=0;
FEvent[j]:=CreateEvent(nil,True, False,nil);
for i:=0 to 4 do
begin
work:=TWorkThread.Create(j);
inc(k);
end;
repeat
code:= waitforsingleobject(FEvent[j], INFINITE);
if Code = WAIT_FAILED then
begin
dec(k);
form1.Label1.Caption:=form1.Label1.Caption+inttostr(k);
resetevent( FEvent[j]);
end
else
if Code = WAIT_OBJECT_0 then
begin
dec(k);
form1.Label1.Caption:=form1.Label1.Caption+inttostr(k);
resetevent( FEvent[j]);
end;
until //有时候执行不到这一步,k无法归零,问题可能是子线程的setevent冲突了
k=0;
form1.Edit1.Text:='OK';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 0 do
begin
manager:=Tmanagerthread.Create(i); //先只创建一个管理线程试试
//sleep(10000);
end;
end;

end.
 
code会不会还有别的值
 
procedure TWorkThread.Execute;
var
l:integer;
begin
Randomize; <<------------
l:=random(100);
sleep(l*100);
setevent(FEvent[n]); //问题可能在这里,是不是两个子线程的setevent时间间隔太短,而下面waitforsingleobject还没有处理完毕?
end;

改成这样之后我试了好几次都没问题
 
另外,repeat里面还存在一个很严重的逻辑问题,就是waitforsingleobject的时候,如果有2个线程同时执行完毕,就无法更改FEvent[j],导致循环无法结束。
每个线程的执行时间越短,同时创建的线程越多,这种错误逻辑出现的概率就越大。
正确的做法是为每个线程设置一个信号量
 
再给你举个形象的例子,就是5个线程结束的顺序如下:
[2] [4] [3] [1] [0]
你的管理线程waitforsingleobject得到的cpu时间片正好在每个线程结束之间,就没有问题。
但是管理线程也可能分不到那么多的时间片,就有问题了。
[2] [4] [3] [1] [0]
| | | | 没有问题( | 为管理线程此刻获得时间片)
[2] [4] [3] [1] [0]
| | 有问题
 
to cactus123456:
对呀,我也觉得是这个问题,你说 &quot;正确的做法是为每个线程设置一个信号量 &quot; ,是不是要定义一个二维数组,FEvent:array[0..1, 0..4] of THandle;那waitforsingleobject不是要用好多的循环,具体该怎么写,麻烦你给点代码
 
二维,一维(m*n)都可以,用信号量的话,循环是必需的,要不怎么访问呀
另外一种的方法就是用OnTerminate
type
TManagerThread = class(TThread)
private
Work:array[0..4] of TWorkThread;
j,k:integer;
procedure proOnTerminate(Sender: TObject);
protected
procedure Execute;override;
public
constructor Create(i:integer);
end;

work:=TWorkThread.Create(j);
work.OnTerminate:=proOnTerminate;

k定义为TManagerThread 的私有变量
在proOnTerminate里k减1 ,k=0; form1.Edit1.Text:='OK'; (注意真正要这个用的时候,一定要加保护Synchronize)

这种方法仅供参考,没有测试过。

下班了,明年见!
 
to cactus123456:
你的第二个方法可以用,但是有局限性!下面的情况就不能解决了:
5个子线程完成某项操作后通知管理线程并自行挂起;当管理线程收到消息,并确认5个线程全部完成后,执行一段过程,再将5个子线程Resume,再次进入等待状态!
这种情况就只能用你说的第一种方法了!我随便写了一下不知道对不对,如果你明天看到的话,帮我查一下,谢谢

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TWorkThread = class(TThread)
private
n:integer;
protected
procedure Execute; override;
public
constructor Create(m:integer);
end;

type
TManagerThread = class(TThread)
private
Work:array[0..4] of TWorkThread;
j:integer;
protected
procedure Execute;override;
public
constructor Create(i:integer);
end;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
manager:array[0..1] of TManagerThread;
public
{ Public declarations }
end;

var
Form1: TForm1;
hSem : array[0..1] of THandle;
implementation

{$R *.dfm}
{ --------------- TWorkThread----------------------------------}
constructor TWorkThread.Create(m:integer);
begin
n:=m;
FreeOnTerminate := True;
inherited create(false);
end;

procedure TWorkThread.Execute;
var
l:integer;
begin
Randomize;
l:=random(10)+5;
form1.Edit2.Text:=form1.Edit2.Text+inttostr(l);
sleep(l*1000);
ReleaseSemaphore(hSem[n],1,Nil);
end;

{ --------------- TManagerThread----------------------------------}

constructor TManagerThread.Create(i:integer);
begin
j:=i;
FreeOnTerminate := True;
inherited create(false);
end;

{procedure TManagerThread.proOnTerminate(Sender: TObject);
begin
dec(k);
end; }

procedure TManagerThread.Execute;
var
i,k:integer;
begin
k:=0;
hSem [j]:=CreateSemaphore(nil,0,1,nil);
for i:=0 to 4 do
begin
work:=TWorkThread.Create(j);
inc(k);
end;
repeat
if waitforsingleobject(hsem[j],INFINITE)=WAIT_OBJECT_0 then
begin
dec(k);
form1.Label1.Caption:=form1.Label1.Caption+inttostr(k);
end;
until
k=0;
CloseHandle(hsem[j]);
form1.Edit1.Text:='OK';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 0 do
begin
manager:=Tmanagerthread.Create(i);
//sleep(10000);
end;
end;

end.
 
接受答案了.
 
后退
顶部