delphi nethttpclient操作cookie
<p>delphi nethttpclient操作cookie</p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uses System.Net.HttpClientComponent,System.Net.HttpClient,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取 cookei </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> <span style="color: rgba(0, 0, 255, 1)">var</span> TCooki <span style="color: rgba(0, 0, 255, 1)">in</span> nethttp.CookieManager.Cookies <span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> nethttp: TNetHTTPClient;</span>
<span style="color: rgba(0, 0, 0, 1)">begin
</span><span style="color: rgba(0, 0, 255, 1)">if</span> str_str(uip, StrRight(TCooki.Domain, Length(TCooki.Domain) - <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)) then
edit1.Text :</span>=<span style="color: rgba(0, 0, 0, 1)"> TCooki.Value;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">memo1.Lines.Add(TCooki.Path + ' | ' + TCooki.Domain + ' | ' + TCooki.Name + ' | ' + TCooki.Value);</span>
<span style="color: rgba(0, 0, 0, 1)">end;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加cookie </span>
<span style="color: rgba(0, 0, 255, 1)">var</span> a := TURI.Create(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://xxxxxx/main.action</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
a.path :</span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> cookie := System.Net.HttpClient.tcookie.Create(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">JSESSIONID=</span><span style="color: rgba(128, 0, 0, 1)">'</span> + Edit1.text + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">;</span><span style="color: rgba(128, 0, 0, 1)">'</span>, aaaa); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 'JSESSIONID=A17E7980BA97270045B829D8CC0EF060;'</span>
nhttp :=<span style="color: rgba(0, 0, 0, 1)"> TNetHTTPClient.Create(nil);
nhttp.CookieManager.Clear;</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除cookie </span>
<span style="color: rgba(0, 0, 0, 1)">nhttp.CookieManager.AddServerCookie(cookie, a);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最简单取得cookie后 克隆控件属性</span>
myHttp.Assign(nHttp); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">直接克隆属性myHttp 复制 nHttp,释放nHttp不会影响myHttp</span>
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">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;
type
TForm1 = class(TForm)
NetHTTPClient1: TNetHTTPClient;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
cookie: TCookie;
begin
for cookie in NetHTTPClient1.CookieManager.Cookies do
begin if (cookie.Domain = 'www.server.com') and (cookie.Name = 'test') then
begin
//
end;
end;
end;
end.
</pre>
</div>
<p> TCookie:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">/// <summary>Cookie record.</summary>
TCookie = record
private
class function StrExpiresToDateTime(const AStrDate: string): TDateTime; static;
public
/// <summary>Cookie Name</summary>
Name: string;
/// <summary>Cookie Value</summary>
Value: string;
/// <summary>Cookie Expires. It's the date when the cookie will expire</summary>
/// <remarks>When Expires is 0 means a session cookie.</remarks>
Expires: TDateTime;
/// <summary>Cookie Domain</summary>
Domain: string;
/// <summary>Cookie Path</summary>
Path: string;
/// <summary>Cookie Secure</summary>
/// <remarks>If True then the cookie will be sent if https scheme is used</remarks>
Secure: Boolean;
/// <summary>Cookie HttpOnly</summary>
/// <remarks>If True then the cookie will not be used in javascript, it's browser dependant.</remarks>
HttpOnly: Boolean;
/// <summary>Return the cookie as string to be send to the server</summary>
function ToString: string;
/// <summary>Return a TCookie parsing ACookieData based on the URI param</summary>
class function Create(const ACookieData: string; const AURI: TURI): TCookie; static;
end;
</pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/10892102.html</p><br><br>
来源:https://www.cnblogs.com/hnxxcxg/p/10892102.html
頁:
[1]