博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TNetHTTPClient演示
阅读量:6473 次
发布时间:2019-06-23

本文共 6185 字,大约阅读时间需要 20 分钟。

TNetHTTPClient演示

TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件)。

unit Unit1;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent, Vcl.StdCtrls;

type

TForm1 = class(TForm)
NetHTTPClient1: TNetHTTPClient;
btnGet: TButton;
Memo1: TMemo;
btnSsl: TButton;
NetHTTPClient2: TNetHTTPClient;
btnPost: TButton;
procedure btnGetClick(Sender: TObject);
procedure NetHTTPClient1RequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
procedure btnSslClick(Sender: TObject);
procedure NetHTTPClient2RequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
procedure btnPostClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var

Form1: TForm1;

implementation

uses System.NetEncoding;

{$R *.dfm}

function UrlDecode(const AStr: AnsiString): AnsiString;
var
Sp, Rp, Cp: PAnsiChar;
s: AnsiString;
begin
SetLength(Result, Length(AStr));
Sp := PAnsiChar(AStr);
Rp := PAnsiChar(Result);
Cp := Sp;
while Sp^ <> #0 do
begin
case Sp^ of
'+':
Rp^ := ' ';
'%':
begin
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <> #0) and (Sp^ <> #0) then
begin
s := AnsiChar('$') + Cp^ + Sp^;
Rp^ := AnsiChar(StrToInt(string(s)));
end;
end;
Cp := Cp;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PAnsiChar(Result));
end;

procedure TForm1.btnGetClick(Sender: TObject);

var
vHttp: TNetHTTPClient;
vUTF8, vGBK: TStringStream;
begin
vHttp := TNetHTTPClient.Create(nil);
vUTF8 := TStringStream.Create('', TEncoding.GetEncoding(65001));
vGBK := TStringStream.Create('', TEncoding.GetEncoding(936));
try
Memo1.Lines.Add('----------------阻塞----------------');
with vHttp do
begin
vUTF8.Clear;
ConnectionTimeout := 2000; // 2秒
ResponseTimeout := 10000; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '65001';
AcceptLanguage := 'zh-CN';
ContentType := 'text/html';
UserAgent := 'Embarcadero URI Client/1.0';
try
Get('http://offeu.com/utf8.txt', vUTF8);
Memo1.Lines.Add('utf8:' + TNetEncoding.URL.UrlDecode(vUTF8.DataString));
except
on E: Exception do
// Error sending data: (12002) 操作超时.
// Error receiving data: (12002) 操作超时
if Copy(E.Message, 1, Pos(':', E.Message) - 1) = 'Error sending data'
then
Memo1.Lines.Add('utf8:连接失败!')
else if Copy(E.Message, 1, Pos(':', E.Message) - 1) = 'Error receiving data'
then
Memo1.Lines.Add('utf8:接收失败,请延长接收超时时间!')
else
Memo1.Lines.Add('utf8:' + E.Message);
end;
vGBK.Clear;
AcceptCharSet := 'gbk';
AcceptEncoding := '936';
AcceptLanguage := 'zh-CN';
ContentType := 'text/html';
UserAgent := 'Embarcadero URI Client/1.0';
Get('http://offeu.com/gbk.txt', vGBK);
Memo1.Lines.Add('gbk:' + string(UrlDecode(AnsiString(vGBK.DataString))));
end;
Memo1.Lines.Add('----------------异步----------------');
with NetHTTPClient1 do
begin
Asynchronous := true;
ConnectionTimeout := 10000; // 10秒
ResponseTimeout := 10000; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '65001';
AcceptLanguage := 'zh-CN';
ContentType := 'text/html';
UserAgent := 'Embarcadero URI Client/1.0';
Get('http://offeu.com/utf8.txt');
end;
finally
vUTF8.Free;
vGBK.Free;
vHttp.Free;
end;
end;

procedure TForm1.btnSslClick(Sender: TObject);

var
vHttp: TNetHTTPClient;
vS: TStringStream;
begin
// 这里用的 APPCODE 是阿里云市场中的api,需要申请。
vHttp := TNetHTTPClient.Create(nil);
vS := TStringStream.Create('', TEncoding.GetEncoding(65001));
try
with vHttp do
begin
Memo1.Lines.Add('--------------SSL阻塞--------------');
vS.Clear;
ConnectionTimeout := 10000; // 10秒
ResponseTimeout := 10000; // 10秒
CustomHeaders['Authorization'] :=
'APPCODE 你申请的appcode';
Accept := 'application/json;';
ContentType := 'application/json; charset=utf-8;';
UserAgent := 'Embarcadero URI Client/1.0';
Get('https://dm-81.data.aliyun.com/rest/160601/ip/getIpInfo.json?'
+ 'ip=60.191.244.5', vS);
Memo1.Lines.Add('ssl:'
+ string(TNetEncoding.URL.UrlDecode(vS.DataString)));
end;
finally
vS.Free;
vHttp.Free;
end;
Memo1.Lines.Add('--------------SSL异步--------------');
with NetHTTPClient2 do
begin
Asynchronous := true;
ConnectionTimeout := 10000; // 10秒
ResponseTimeout := 10000; // 10秒
CustomHeaders['Authorization'] :=
'APPCODE 你申请的appcode';
Accept := 'application/json;';
ContentType := 'application/json; charset=utf-8;';
UserAgent := 'Embarcadero URI Client/1.0';
Get('https://dm-81.data.aliyun.com/rest/160601/ip/getIpInfo.json?'
+ 'ip=60.191.244.5');
end;
end;

procedure TForm1.btnPostClick(Sender: TObject);

var
vHttp: TNetHTTPClient;
vS: TStringStream;
vList: TStrings;
begin
vHttp := TNetHTTPClient.Create(nil);
vList := TStringList.Create;
vS := TStringStream.Create;
try
Memo1.Lines.Add('----------------Post阻塞----------------');
vS.Clear;
with vHttp do
begin
ConnectionTimeout := 2000; // 2秒
ResponseTimeout := 10000; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '65001';
AcceptLanguage := 'zh-CN';
ContentType := 'text/html';
UserAgent := 'Embarcadero URI Client/1.0';
vList.Clear;
vList.Values['id'] := 'test';
vList.Values['pwd'] := 'test';
vList.Values['cmd'] := '1';
try
Post('http://60.191.220.219:8090', vList, vS); // utf8进gbk出
// Memo1.Lines.Add('post:' + TNetEncoding.URL.UrlDecode(vS.DataString));
Memo1.Lines.Add('post:' + vS.DataString);
except
on E: Exception do
// Error sending data: (12002) 操作超时.
// Error receiving data: (12002) 操作超时
if Copy(E.Message, 1, Pos(':', E.Message) - 1) = 'Error sending data'
then
Memo1.Lines.Add('post:连接失败!')
else if Copy(E.Message, 1, Pos(':', E.Message) - 1) = 'Error receiving data'
then
Memo1.Lines.Add('post:接收失败,请延长接收超时时间!')
else
Memo1.Lines.Add('post:' + E.Message);
end;
end;
finally
vS.Free;
vList.Free;
vHttp.Free;
end;
end;

procedure TForm1.NetHTTPClient1RequestCompleted(const Sender: TObject;

const AResponse: IHTTPResponse);
begin
Memo1.Lines.Add('utf8:' + TNetEncoding.URL.UrlDecode(
AResponse.ContentAsString(TEncoding.GetEncoding(65001))));
end;

procedure TForm1.NetHTTPClient2RequestCompleted(const Sender: TObject;

const AResponse: IHTTPResponse);
begin
Memo1.Lines.Add('ssl:' + TNetEncoding.URL.UrlDecode(
AResponse.ContentAsString(TEncoding.GetEncoding(65001))));
end;

end.

转载地址:http://wfvko.baihongyu.com/

你可能感兴趣的文章
ES6中Promise封装ajax的写法
查看>>
初次使用 VUX
查看>>
javascript 字符串转数字的简便写法
查看>>
0404《构建之法》第四章读后感
查看>>
html之div始终停留在屏幕中间部分
查看>>
AsyncTask的缺陷
查看>>
Spring中jdbcTemplate的用户实例
查看>>
[模板] 快速傅里叶变换/FFT/NTT
查看>>
DecimalFormat 数据格式设置 SimpleDateFormat时间格式的用法介绍 --转载
查看>>
Android 的Margin和Padding属性以及支持的长度单位
查看>>
653. Two Sum IV - Input is a BST
查看>>
HDU ACM 1050 Moving Tables
查看>>
Django templates加载css/js/image等静态资源
查看>>
Eclipse C + GTK2.0环境构筑
查看>>
caffe solver
查看>>
Rhel6-heartbeat+lvs配置文档
查看>>
[CF340D]Bubble Sort Graph/[JZOJ3485]独立集
查看>>
ORACLE分科目统计每科前三名的学生的语句
查看>>
第一次冲刺--查看活动详情用户场景分析
查看>>
0317复利计算的回顾与总结
查看>>