idhttp在post表单的问题
1、要post的页面里的变量有的是必填项,有的是可选的;是不是只要post页面里必填的变量就可以了?
2、如何取得post后返回的数据?
3、有没有比idhttp好用的控件?
[解决办法]
就这一个unit,没别的了,我用的是XE2
unit Unit2;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls, IdCookieManager;type TForm2 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; HTTP: TIdHTTP; IdCookieManager1: TIdCookieManager; Button3: TButton; Memo2: TMemo; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Button2Click(Sender: TObject); private Html: string; postURL: string; cssList, inputList: TStrings; function GetSubString(BeginStr, EndStr: string): string; function SearchCss(InputField: string): Boolean; public { Public declarations } end;var Form2: TForm2;implementation{$R *.dfm}const urlGet = 'http://www.eset.eu/download/ess-trial-form';procedure TForm2.Button1Click(Sender: TObject);var sURL: string;begin Html := HTTP.Get(urlGet); // 取出主页 // 得到Post页 sURL := GetSubString('<iframe src="', '" width="100%"'); Http.Request.Referer := urlGet; // 第二次Get, 取到被加密的 Form sURL := 'Http://www.eset.eu' + sURL; postURL := sURL; Html := Http.Get(sURL); Memo1.Text := Html;end;procedure TForm2.Button2Click(Sender: TObject);var Dest: TStringStream;begin Dest := TStringStream.Create('', TEncoding.GetEncoding(936)); Http.Post(postURL, inputList, Dest); Dest.Seek(0, 0); Memo2.Lines.LoadFromStream(Dest); Dest.Free; if Pos('<div>A new license has been sent to', Memo2.Text) > 0 then ShowMessage('OK');end;procedure TForm2.Button3Click(Sender: TObject);var cssLine, inputField, fieldValue: string;begin cssList.Clear; Html := Memo1.Text; Html := GetSubString('<style type="text/css">', '</style>'); repeat cssLine := GetSubString('#', '}'); if cssLine <> '' then cssList.Add(cssLine); until (cssLine = ''); inputList.Clear; Html := Memo1.Text; repeat inputField := GetSubString('<input type="text" name="', '"'); if inputField <> '' then begin fieldValue := GetSubString('value="', '"'); if fieldValue = '' then begin if SearchCss(inputField) then inputlist.Add(inputField + '=' + Edit1.Text) else inputList.Add(inputField + '='); end else begin inputList.Add(inputField + '=' + fieldValue); end; end; until (inputField=''); // add Submit line inputField := GetSubString('<input type="submit" name="', '"'); fieldValue := GetSubString('value="', '"'); inputList.Add(inputField + '=' + fieldValue); // add Hide Line inputField := GetSubString('<input type="hidden" name="', '"'); fieldValue := GetSubString('value="', '"'); inputList.Add(inputField + '=' + fieldValue); Memo2.Lines.Assign(inputList); ShowMessage('Down');end;procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin cssList.Free; inputList.Free;end;procedure TForm2.FormCreate(Sender: TObject);begin cssList := TStringList.Create; inputList := TStringList.Create;end;function TForm2.GetSubString(BeginStr, EndStr: string): string;var nPos: Integer;begin nPos := Pos(BeginStr, Html); if nPos = 0 then begin Result := ''; Exit; end; Html := Copy(Html, nPos + Length(BeginStr) , MaxInt); nPos := Pos(EndStr, Html); if nPos = 0 then nPos := MaxInt; Result := Copy(Html, 1, nPos -1); Html := Copy(Html, nPos + Length(EndStr), MaxInt);end;function TForm2.SearchCss(InputField: string): Boolean;var i: Integer;begin Result := False; for I := 0 to cssList.Count -1 do begin if Pos(InputField, cssList.Strings[i]) > 0 then begin if Pos('display: block;', cssList.Strings[i]) > 0 then Result := True; Break; end; end;end;end.