idhttp post数据的问题

edisonli 2008-10-20 11:15:44
我用同一个idhttp组建,执行GET和POST操作,GET没问题,POST始终不对(获取数据包,失踪header不正确),请高手执教
procedure TForm1.Button1Click(Sender: TObject);
var
xx:string;
xxx:tstrings;
yy:TMemoryStream;
jpg:Tjpegimage;
begin
idhttp1.Request.Connection:='Keep-Alive';
idhttp1.Request.CacheControl:='no-cache';
xxx:=tstringlist.Create;
yy := TMemoryStream.Create ;
xx:=IdHTTP1.Get('http://login.tom.com/captcha/drawme.php?type=6');
xxx:=SplitString(xx,'=');
IdHTTP1.Get('http://login.tom.com/captcha/draw.php?post_key='+stringreplace(xxx[8],';expires','',[rfReplaceAll])+'&type=6',yy);
if StreamType(yy)='JPEG' then begin
yy.Position:=0;
try
Jpg:=tjpegimage.Create;
Jpg.LoadFromStream(yy);
image1.Picture.Graphic:= Jpg;
finally
Jpg.Free;
end;
end else if StreamType(yy)='BMP' then begin
yy.Position:=0;
image1.Picture.Bitmap.LoadFromStream(yy);
end else if StreamType(yy)='PNG' then begin

end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
ParaList:tStringList;
str:string;
begin
ParaList:=tstringlist.Create;
idhttp1.Request.Connection:='Keep-Alive';
idhttp1.Request.CacheControl:='no-cache';
idhttp1.request.contenttype:='application/x-www-form-urlencoded';
try
ParaList.Add('funcid=reguser');
ParaList.Add('rf=060702');
ParaList.Add('refer=http://mail.tom.com');
ParaList.Add('year=1980');
ParaList.Add('month=1');
ParaList.Add('day=1');
ParaList.Add('uid=fsdads35sdf');
ParaList.Add('password=111111qq');
ParaList.Add('confirm_password=111111qq');
ParaList.Add('safemail=ad@asdf.com');
ParaList.Add('authcode='+edit1.Text);
ParaList.Add('argee=1');
str:=idhttp1.Post('http://bjcgi.tom.com/cgi-bin/tom_reg.cgi?rf=060702',paralist);
WB_LoadHTML(WebBrowser1,str);
finally
ParaList.Free;
end;

end;

以上代码同样设置idhttp1.Request.Connection:='Keep-Alive';
但是GET获取的数据包header是正确的Connection: Keep-Alive,但是POST获取的数据包确是Connection: close,包括idhttp1.Request.CacheControl:='no-cache';也没有生效
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
guanzhongw 2012-05-17
  • 打赏
  • 举报
回复
unit dt;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, RzEdit , Unit_Hashtable, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP , uLkJSON , IdCoderMIME , idUri ;

type
TForm1 = class(TForm)
Button1: TButton;
RzMemo1: TRzMemo;
Button2: TButton;
IdHTTP1: TIdHTTP;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function URLencode(str:string):string;
var
i:integer;
temp:string;
begin
for i:= 1 to length(str) do
begin
temp:=temp+'%'+inttohex(integer(str[i]),0);
end;
result:=temp;
end;

function URLdecode(str:string):string;
var
temp:string;
i:integer;
begin
setlength(temp,length(str) div 3);
i:=1;
while(i<length(str)) do
begin
temp[(i+2)div 3]:=char(strtoint('$'+str[i+1]+str[i+2]));
inc(i,3);
end;
Result:=temp;
end;



function HTTPEncode(const AStr: string): string;
const
NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> #0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp, 2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

procedure TForm1.Button1Click(Sender: TObject);
var

filename1,filename2,str :String ;

ta:THashTable ;

i:Integer ;

begin

i := 0 ;

ta := THashTable.Create();

Try

for i := 0 to 1000000 do
Begin
str := Formatdatetime('yyyymmddhhmmsszzz', now) ;
ta.Put(str,str);
// RzMemo1.Lines.Add(str) ;
End ;

Showmessage( ta.Get(str) ) ;

Showmessage( IntToStr( i ) ) ;

str := ta.Get('123') ;

Showmessage( str ) ;


Except
On E:Exception Do
Begin
ShowMessage( e.Message ) ;
End;
End ;

ta.Destroy;


end;

procedure TForm1.Button2Click(Sender: TObject);
var
i,j:integer;
ParaList:tStringList;

// json 发送短信所需
js :TlkJSONobject ;

rec:TlkJSONobject ;

Data:TlkJSONobject;

Mobile:TlkJSONobject;

Mobiles:TlkJSONlist;

xs:TlkJSONbase;

strMac , tmp , str : String ;

bResp :boolean ;

retjson:TlkJSONobject;

MIMEdecode :TIdDecoderMIME;

IdEncoderMIME1: TIdEncoderMIME;

begin

//////////////////////包头//////////////////////////////////////////
js := TlkJSONobject.Create(True);

js.Add('Sequence',formatdatetime('yyyymmddhhmmsszzz', now)); // 包序号

js.Add('BrokerID','102601');

js.Add('MAC','123456') ;

js.Add('SendTime',formatdatetime('yyyy-mm-dd hh:mm:ss', now));

Data := TlkJSONobject.Create(True);

// 可以放多个短信

rec := TlkJSONobject.Create(True); // 创建记录

rec.Add('MessageID', formatdatetime('yyyymmddhhmmsszzz', now) ); // 消息ID

rec.Add('OrganId',0); // 机构ID

rec.Add('OrganUserID',0); // 用户ID

rec.Add('SubmitTime',Formatdatetime('yyyy-mm-dd hh:mm:ss', now)); // 提交时间

rec.Add('Priority','0'); // 优先级别

rec.Add('Channel_Code','100'); // 通道

IdEncoderMIME1 := TIdEncoderMIME.Create(Nil);

str := IdEncoderMIME1.Encode('(测试)尊敬的刘建华先生,您好!您申请的尾号0025保单的投保人资料变更已处理完毕,如您有疑问请致电4006999100,感谢您的信任!') ;

FreeAndNil( IdEncoderMIME1 ) ;

rec.Add('MsgContent', URLencode( str ) ); // 信息内容 base64编码

Mobiles := TlkJSONlist.Create; // 手机数组

// 可以放多个手机号码

Mobile:= TlkJSONobject.Create(true); // 手机号码属性

// 手机号码1
Mobile.Add('Number','13016233706'); // 手机号码

Mobile.Add('Operator','0'); // 运营商

Mobile.Add('SendNumber','0'); // 扩展码

Mobile.Add('ServiceNumber','0'); // 接入码

Mobiles.Add( TlkJSON.GenerateText(Mobile) ) ;

FreeAndNil( Mobile ) ;

//////////////////////////////////////////////////////////////////
rec.Add('Mobiles',Mobiles); // 把手机号码增加到记录中

Data.Add( '0' , TlkJSON.GenerateText(rec)) ; // 加到数据包中( 从 0 - n )

Try

Mobiles.Delete(0);

Except
On E:Exception Do
Begin
Showmessage( e.Message ) ;
End;
End;

FreeAndNil( rec ) ;


// 发送的数据
js.Add('Data',TlkJSON.GenerateText(Data)) ;

ParaList := TStringList.Create ;

IdHTTP1.ProxyParams.ProxyServer := '128.1.241.11' ;

IdHTTP1.ProxyParams.ProxyPort := 80 ;

ParaList.Add(TlkJSON.GenerateText( js )) ;

str := IdHTTP1.Post('http://113.59.109.38:18988/GetJsonSmsContent.aspx?rmd=123456&id=102601',ParaList) ;

ShowMessage( str ) ;

FreeAndNil( js ) ;
FreeAndNil( Data ) ;
FreeAndNil( ParaList ) ;


end;

end.
edisonli 2008-10-21
  • 打赏
  • 举报
回复
哭啊,没人能解吗
edisonli 2008-10-21
  • 打赏
  • 举报
回复
高手在吗,支点招啊
pilicat 2008-10-20
  • 打赏
  • 举报
回复
嗯,收藏下来,研究一下.

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧