TNetHTTPClient 使用
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;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
NetHTTPClient2: TNetHTTPClient;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure NetHTTPClient1RequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
procedure Button2Click(Sender: TObject);
procedure NetHTTPClient2RequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
procedure Button3Click(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^ <> # do
begin
case Sp^ of
'+':
Rp^ := ' ';
'%':
begin
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <> #) and (Sp^ <> #) 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.Button1Click(Sender: TObject);
var
vHttp: TNetHTTPClient;
vUTF8, vGBK: TStringStream;
begin
vHttp := TNetHTTPClient.Create(nil);
vUTF8 := TStringStream.Create('', TEncoding.GetEncoding());
vGBK := TStringStream.Create('', TEncoding.GetEncoding());
try
Memo1.Lines.Add('----------------阻塞----------------');
with vHttp do
begin
vUTF8.Clear;
ConnectionTimeout := ; // 2秒
ResponseTimeout := ; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '';
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, , Pos(':', E.Message) - ) = 'Error sending data'
then
Memo1.Lines.Add('utf8:连接失败!')
else if Copy(E.Message, , Pos(':', E.Message) - ) = 'Error receiving data'
then
Memo1.Lines.Add('utf8:接收失败,请延长接收超时时间!')
else
Memo1.Lines.Add('utf8:' + E.Message);
end;
vGBK.Clear;
AcceptCharSet := 'gbk';
AcceptEncoding := '';
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 := ; // 10秒
ResponseTimeout := ; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '';
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.Button2Click(Sender: TObject);
var
vHttp: TNetHTTPClient;
vS: TStringStream;
begin
// 这里用的 APPCODE 是阿里云市场中的api,需要申请。
vHttp := TNetHTTPClient.Create(nil);
vS := TStringStream.Create('', TEncoding.GetEncoding());
try
with vHttp do
begin
Memo1.Lines.Add('--------------SSL阻塞--------------');
vS.Clear;
ConnectionTimeout := ; // 10秒
ResponseTimeout := ; // 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 := ; // 10秒
ResponseTimeout := ; // 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.Button3Click(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 := ; // 2秒
ResponseTimeout := ; // 10秒
AcceptCharSet := 'utf-8';
AcceptEncoding := '';
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'] := '';
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, , Pos(':', E.Message) - ) = 'Error sending data'
then
Memo1.Lines.Add('post:连接失败!')
else if Copy(E.Message, , Pos(':', E.Message) - ) = '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())));
end; procedure TForm1.NetHTTPClient2RequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
Memo1.Lines.Add('ssl:' + TNetEncoding.URL.UrlDecode(
AResponse.ContentAsString(TEncoding.GetEncoding())));
end; end.
TNetHTTPClient 使用的更多相关文章
- TNetHTTPClient演示
TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...
- TNetHttpClient支持异步访问(Delphi 10.1 Berlin,红鱼儿的博客)
Delphi 10.1进一步改进自Delphi 10带来的Http访问控件TNetHttpClient,支持异步访问,同时增加ConnectionTimeout及ResponseTimeout两个超时 ...
- Delphi 10.3.1 TNetHttpClient在多线程中存在的问题及解决方法。
Delphi 10.3.1发布了,对10.3.0存在的各种问题,做了大量的修正.但听高勇说TNetHttpClient在多线程中存在问题,今天做了一下测试,确实如此,看来,还需要官方进一步修正! 具体 ...
- TNetHttpClient的用法
TNetHttpClient的用法 TNetHttpClient是DELPHI XE8新增加的控件. 在之前,我们一般都是使用IDHTTP控件,但在安卓.IOS等非WINDOWS平台,IDHTTP访问 ...
- 使用http.sys,让delphi 的多层服务飞起来
核心提示:一直以来,delphi 的网络通讯层都是以indy 为主,虽然indy 的功能非常多,涉及到网络服务的各个方面,但是对于大多数多层服务来说,就是需要一个快速.稳定.高效的传输层.Delphi ...
- FMX手机app,如何下载网站图片而不卡界面
你用的版本? 你应该关注下delphi 更新说明第一方法: 可以用线程.第二方法: TNetHTTPClient 已经支持异步 TThread.CreateAnonymousThread( proce ...
- 移动开发的框架(用Firepower,不用listview,超快) good
我是通过http传送xml后台是阿帕奇的http server,后台可以用delphi或php 都可以.用post 刚才试了试自带的TNetHttpClient,感觉还好,代码封装也不算深,收发数据也 ...
- 使用kbmmw 的REST 服务实现上传大文件
我们在使用kbmmw的REST 服务时,经常会下载和上传大文件.例如100M以上的.kbmmw的rest服务中 提供标准的文件下载,上传功能,基本上就是打开文件,发送,接收,没有做特殊处理.这些对于文 ...
- kbmmw 做REST 服务签名认证的一种方式
一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...
随机推荐
- ajax和iframe区别
ajax和iframe https://segmentfault.com/a/1190000011967786 ajax和iframe的区别 1.都是局部刷新 2.iframe是同步的,而ajax是异 ...
- linux文件管理之解压缩
文件的压缩与解压缩 Linux文件压缩工具有:gzip.bzip2.rar.7zip.lbzip2.xz.lrzip.PeaZip.arj等.============================= ...
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...
- android--------Socket的简单了解
Socket目录 Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连 ...
- KDD Cup 99网络入侵检测数据的分析
看论文 该数据集是从一个模拟的美国空军局域网上采集来的 9 个星期的网络连接数据, 分成具有标识的训练数据和未加标识的测试数据.测试数据和训练数据有着不同的概率分布, 测试数据包含了一些未出现在训练数 ...
- BigInteger 类 和 BigDecimal 类
一 .BigInteger BigInteger类在计算和处理任意大小的整数方面是很有用的. BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的. BigInte ...
- MongoDB\BSON\UTCDateTime::toDateTime
示例# 1 MongoDB \ BSON \ UTCDatetime:toDateTime()例子 <?php $utcdatetime = new MongoDB\BSON\UTCDateTi ...
- activiti实战系列之动态表单 formService 自定义变量类型
目前Activiti默认支持的类型有String,long,enum,date,boolean,collection 要自定义字段类型,首先需要表单类型解析类 /** * @Author:LJ * @ ...
- java项目中登陆时记住密码
1.在登陆的时候记住密码,不知自动登陆: 2.登陆页面,填写用户名,密码,点击记住密码,下次进入登陆页面的时候,填写同样的用户名,密码自动填充(在不一次会话的情况下也就是说在不关闭浏览器的情况下): ...
- 牛客第二场A-run
链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 White Cloud is exercising in the playground. Wh ...