用户夜猫子 發表於 2020-8-6 17:26:00

TRestClient

<p>TRestClient</p>
<p>Delphi XE6开始,REST Client系列元件渐渐开始成为&nbsp;Delphi&nbsp;团队的重点开发项目之一。TRESTClient, TRESTRequest, TRESTResponse 系列组合。</p>
<div class="cnblogs_Highlighter">
<div><span class="hljs-keyword">var</span></div>
<div>&nbsp; jo: TJSONObject;</div>
<div><span class="hljs-keyword">begin</span></div>
<div>&nbsp; jo:= TJSONObject.Create;</div>
<div>&nbsp; <span class="hljs-keyword">try</span></div>
<div>&nbsp; &nbsp; ResetRESTComponentsToDefaults;</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; RESTClient.BaseURL := <span class="hljs-string">'http://ynmiddle:9999/';</span></div>
<div>&nbsp; &nbsp; RESTClient.Authenticator := HTTPBasicAuthenticator1;</div>
<div>&nbsp; &nbsp; RESTClient.ContentType := <span class="hljs-string">'application/x-www-form-urlencoded';</span></div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; RESTRequest.Method := TRESTRequestMethod.rmPOST;</div>
<div>&nbsp; &nbsp; RESTRequest.Resource := <span class="hljs-string">'oauth/token';</span></div>
<div>&nbsp;</div>
<div>
<div>&nbsp; &nbsp; HTTPBasicAuthenticator1.Username := <span class="hljs-string">'orbid-hml';</span></div>
<div>&nbsp; &nbsp; HTTPBasicAuthenticator1.Password := EmptyStr;</div>
</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; jo.AddPair(<span class="hljs-string">'scope', <span class="hljs-string">'product:show stock:save stock:update warehouse:list warehouse:save warehouse:show '+</span></span></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-string">'warehouse:update partnerOrder:show partnerOrder:list partnerOrder:update');</span></div>
<div>&nbsp; &nbsp; jo.AddPair(<span class="hljs-string">'grant_type', <span class="hljs-string">'client_credentials');</span></span></div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; RESTRequest.Params.Clear;</div>
<div>&nbsp; &nbsp; RESTRequest.Params.AddHeader(<span class="hljs-string">'authorization', <span class="hljs-string">'Basic '+Edt_Base64.Text);</span></span></div>
<div>&nbsp; &nbsp; RESTRequest.AddBody(jo);</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; RESTRequest.Execute;</div>
<div>&nbsp; &nbsp;&nbsp;memLog.Text:=RESTResponse1.Content;</div>
<div>&nbsp; <span class="hljs-keyword">finally</span></div>
<div>&nbsp; &nbsp; FreeAndNil(jo);</div>
<div>&nbsp; <span class="hljs-keyword">end;</span></div>
<div><span class="hljs-keyword">end;</span></div>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">procedure TForm1.Button4Click(Sender: TObject);
var
url,Timestamp,Signature,seckey: string;
param:Tstringlist;
begin
url:='https://mws.amazonservices.com/Orders/2013-09-01';


self.NetHTTPRequest1.Accept := 'text/html, application/xhtml+xml, */*';
self.NetHTTPRequest1.url := url;
self.NetHTTPRequest1.MethodString := 'POST';
seckey:='seckey';
param:=Tstringlist.Create;
param.Add('AWSAccessKeyId=AWSAccessKeyId');
param.Add('Action=GetOrder');
param.Add('SellerId=SellerId');
param.Add('MWSAuthToken=MWSAuthToken');
param.Add('SignatureVersion=2');
param.Add('Timestamp='+DateToISO8601(now,true));
param.Add('Version=2013-09-01');
param.Add('SignatureMethod=HmacSHA256');
param.Add('AmazonOrderId.Id.1=AmazonOrderId.Id.1');
Signature:=system.hash.THashSHA2.GetHMAC('mws.amazonservices.com/Orders/2013-09-01'+param.Text,seckey);
Signature:=Data.Cloud.CloudAPI.Encode64(Signature);
param.Add('Signature='+Signature);
edit1.Text:=signature;
edit2.Text:=system.hash.THashSHA2.GetHMAC(trim(memo1.Text),seckey);
//memo1.Text:='mws.amazonservices.com/Orders/2013-09-01'+param.Text;

//Signature:=Data.Cloud.CloudAPI.TCloudSHA256Authentication.GetHashSHA256Hex('mws.amazonservices.com/Orders/2013-09-01'+param.Text);


self.Memo2.Text := self.NetHTTPRequest1.Post(url, param).ContentAsString(tencoding.UTF8);

end;
</pre>
</div>
<p>  </p>
<p>  </p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/13447634.html</p><br><br>
来源:https://www.cnblogs.com/hnxxcxg/p/13447634.html
頁: [1]
查看完整版本: TRestClient