DELPHI XE 10.3 调用API SSL 处理 HTTP Basic的验证 传输内容汉字乱码 处理办法
SSL 处理需要程序文件中放libeay32.dll、ssleay32.dll 文件unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils,
System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
StdCtrls,IdHTTP, IdTCPConnection, IdTCPClient, IdBaseComponent, IdComponent,
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;
type
TMain_API_Form = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function SendMsg(Msg : string) : string;
end;
var
Main_API_Form: TMain_API_Form;
implementation
{$R *.dfm}
{ TMain_API_Form }
procedure TMain_API_Form.Button1Click(Sender: TObject);
var
acceptInfo : string;
begin
//向API发送信息,并获取返回
acceptInfo := SendMsg(Edit1.Text);
//将信息在界面上显示
Memo1.Lines.Add(acceptInfo);
end;
function TMain_API_Form.SendMsg(Msg: string): string;
var
idhttp :TIdHTTP;
url,ResquestStr,ResponseStr : string;
ResquestStream,ResponseStream : TStringStream;
begin
Result := '';
idhttp := IdHTTP1;
idhttp.Request.ContentType := 'application/json';
idhttp.Request.Accept := 'application/json';
idhttp.Request.UserName:='**********';
idhttp.Request.Password:='***************';
idhttp.Request.BasicAuthentication:=True;
ResquestStr :=Msg;
//将传递的信息,写入请求流
ResquestStream := TStringStream.Create(ResquestStr, TEncoding.GetEncoding(65001)); //此处是处理中文乱码问题indy10控件的UTF8转码方法会出现汉字乱码
ResponseStream := TStringStream.Create('');
ShowMessage(ResquestStr);
url := 'https://kerrywines.jinshuju.com/api/v1/forms/RZlOkN';
try
try
//发起请求
idhttp.Post(url,ResquestStream,ResponseStream);
except
on e: Exception do
begin
ShowMessage('出现异常:' + e.Message);
end;
end;
//获取响应的信息
ResponseStr := ResponseStream.DataString;
//响应的信息需要进行 UTF8 解密
ResponseStr := UTF8Decode(ResponseStr);
Result := ResponseStr;
finally
idhttp.Free;
ResquestStream.Free;
ResponseStream.Free;
end;
end;
end.
页:
[1]