TNetHttpClient的用法
TNetHttpClient的用法
TNetHttpClient是DELPHI XE8新增加的控件。
在之前,我们一般都是使用IDHTTP控件,但在安卓、IOS等非WINDOWS平台,IDHTTP访问HTTPS却不行了。
大家知道INDY的SSL访问局限于WINDOWS平台,并不支持跨平台HTTPS访问。
鉴于以上原因,所以EMB才推出了TNetHttpClient。
TNetHttpClient既可以阻塞(如同INDY),又可以异步(这就很全面了)。
TNetHttpClient不再和INDY一样依赖OPENSSL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 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^ <> #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.Button1Click(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.Button2Click(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.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 := 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. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/10892054.html
分类:
NetHttpClient
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?