不尽江河 發表於 2019-5-20 09:12:00

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>&nbsp;</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;">/// &lt;summary&gt;Cookie record.&lt;/summary&gt;
TCookie = record
private
    class function StrExpiresToDateTime(const AStrDate: string): TDateTime; static;
public
    /// &lt;summary&gt;Cookie Name&lt;/summary&gt;
    Name: string;
    /// &lt;summary&gt;Cookie Value&lt;/summary&gt;
    Value: string;
    /// &lt;summary&gt;Cookie Expires. It's the date when the cookie will expire&lt;/summary&gt;
    /// &lt;remarks&gt;When Expires is 0 means a session cookie.&lt;/remarks&gt;
    Expires: TDateTime;
    /// &lt;summary&gt;Cookie Domain&lt;/summary&gt;
    Domain: string;
    /// &lt;summary&gt;Cookie Path&lt;/summary&gt;
    Path: string;
    /// &lt;summary&gt;Cookie Secure&lt;/summary&gt;
    /// &lt;remarks&gt;If True then the cookie will be sent if https scheme is used&lt;/remarks&gt;
    Secure: Boolean;
    /// &lt;summary&gt;Cookie HttpOnly&lt;/summary&gt;
    /// &lt;remarks&gt;If True then the cookie will not be used in javascript, it's browser dependant.&lt;/remarks&gt;
    HttpOnly: Boolean;

    /// &lt;summary&gt;Return the cookie as string to be send to the server&lt;/summary&gt;
    function ToString: string;
    /// &lt;summary&gt;Return a TCookie parsing ACookieData based on the URI param&lt;/summary&gt;
    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]
查看完整版本: delphi nethttpclient操作cookie