Delphi7下實現HTTP的Post操作

來源:互聯網
上載者:User

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, StdCtrls, Buttons, OverbyteIcsWndControl, OverbyteIcsHttpProt;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function  httpPost(postUrl:string;Params:TStrings):string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  url : string;
  Params:   TStrings;
begin
  Params   :=   TStringList.Create;
  Params.Add('username=kenter1983');
  Params.Add('password=111111');
  url := 'http://www.cnlive.com/index/?action=login';
  ShowMessage(httpPost(url,Params));

end;

function  TForm1.httpPost(postUrl:string;Params:TStrings):string;
var
  idhtp1:   TIdHTTP;
begin
  idhtp1:=   TidHTTp.create(self);
  idhtp1.AllowCookies:=True;
  idhtp1.HTTPOptions:=[hoForceEncodeParams];
  idhtp1.ProtocolVersion:=pv1_1;
  idhtp1.Request.ContentType:='application/x-www-form-urlencoded';
  idhtp1.Request.CacheControl:='no-cache';  
  idhtp1.Request.UserAgent:='User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1';
  idhtp1.Request.Accept:='Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  idhtp1.Request.AcceptEncoding:='Accept-Encoding=gzip,deflate';
  idhtp1.Request.AcceptCharSet:='Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7';
  idhtp1.Request.Connection:='Connection=keep-alive';
  try 
    result := idhtp1.Post(postUrl,Params);
  except
    Result := 'error';
  end;
end;

end.

檔案流

Const
  CRLF = #13#10;
var
  s,s1,filename:String;
  response:TStringStream;
  source,source1:TMemoryStream;
  Url:string;
  i,cnt:integer;
begin
  idhttp.Request.ContentType:='multipart/form-data';
  Response := TStringStream.Create('');
  url:='Http://'+host+dir;
  cnt:=files.Count;
  if (not dead) then
    begin
     for i:=0 to cnt-1 do
      begin
      filename:=files[i];
      if fileexists(filename) then
       begin
        try
        S := '-----------------------------7cf1d6c47c' + CRLF +
             'Content-Disposition: form-data; name="file1"; filename="'+filename+'"'+CRLF +
             'Content-Type: application/octet-stream' + CRLF + CRLF;
        //上傳檔案內容
        s1:='file one content. Contant-Type can be application/octet-stream or if'+
            'you want you can ask your OS fot the exact type.' + CRLF +
            '-----------------------------7cf1d6c47c' + CRLF + //分界符,用於分隔表單(Form)中的各個域
            'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
            'hello2' + CRLF +
            '-----------------------------7cf1d6c47c--';
        //提交的下一個表單內容域的內容
        s1:=CRLF +'-----------------------------7cf1d6c47c' + CRLF +
            'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
            'hello2' + CRLF +
            '-----------------------------7cf1d6c47c--';
        Source := TMemoryStream.Create;
        Source1 := TMemoryStream.Create;
        Source1.LoadFromFile(filename);
        Response:=TStringStream.Create('') ;
        Response.CopyFrom(source1,source1.Size);
        s:=s+Response.DataString;//因為只能傳字串
        Source.Position :=0;
        Source.Write(s[1],length(s));
        Source.Position :=source.Size ;
        Source.Write(s1[1],length(s1));
        Response.Position :=0;
        try
          idHTTP.Post(url, Source, Response);
        finally
          if not uploadsuccess(Response.DataString) then
           begin
            dead:=true;
            self.idhttp.Disconnect;
           end;
          Source.Free;
          Response.Free;
        end; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.