delphi IdHTTP post 提交 json  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi IdHTTP post 提交 json


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdTCPConnection, IdTCPClient, IdHTTP,
  IdBaseComponent, IdComponent, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    btnPost: TButton;
    IdHTTP1: TIdHTTP;
    procedure FormCreate(Sender: TObject);
    procedure btnPostClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnPostClick(Sender: TObject);
var
  postStream: TStringStream;
  AEnctryRespose: string;
  url: string;
  aEnctryPost: string;
begin
  url := 'https:/xxx.xx';
  aEnctryPost := '{"username":"test","password":"1"}';
  postStream := TStringStream.Create(aEnctryPost);
  try
    postStream.Position := 0;
    AEnctryRespose := IdHTTP1.Post(url, postStream);
    ShowMessage(AEnctryRespose);//post后的返回内容
  finally
    IdHTTP1.Disconnect;
    FreeAndNil(postStream);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IdHTTP1 := TIdHTTP.Create(nil);
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
  IdHTTP1.HandleRedirects := True;
  IdHTTP1.Request.ContentType := 'application/json';//设置交互方式为json格式
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042906999588013 seconds