GVKun编程网logo

IOS 中 http 请求使用 cookie(http请求中的cookie)

1

对于IOS中http请求使用cookie感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解http请求中的cookie,并且为您提供关于Cookie不会通过`Set-Cookie`Header

对于IOS 中 http 请求使用 cookie感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解http请求中的cookie,并且为您提供关于Cookie 不会通过`Set-Cookie` Header 在 Chrome 中设置 我在前端 (Next.JS) 中通过 Express.JS 设置 cookie 时遇到了一些问题我找到了答案、Cookie 域问题:使用带有 jwt 令牌的 cookie 进行身份验证 - 无法为 cookie、cookie、vue-cookie、js-cookie、Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2的宝贵知识。

本文目录一览:

IOS 中 http 请求使用 cookie(http请求中的cookie)

IOS 中 http 请求使用 cookie(http请求中的cookie)

 一直以为 ios 的 http 请求这块很简单应该不支持记录,保存,或者使用 cookie,可是想当然归想当然,真用的时候,真研究了一下发现还真强大。经过一番的研究简单说一下我的理解:当你访问一个网站时,不管你愿意或者不愿意,NSURLRequest 都会帮你主动记录下来你访问的站点设置的 cookie,而且很负责任的,当你下次再访问这个站点时,NSURLRequest 会拿着上次保存下来了的 cookie 继续去请求。这规律同样适用于 ASIHTTPRequest。所以当你做一些基于认证的网络请求时,cookie 不失为一个完美的解决方案。

那么怎么查看 cookie 呢?很简单:

1
2
3
4
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}

这样就列出了所有已保存的 cookie, 如果当前为空怎么办呢?随便请求一个 url 喽。

1
2
3
4
5
6
7
8
9
10
11
12
  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blog.cnrainbird.com"]
                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                                       timeoutInterval:3];
  
  [NSURLConnection sendSynchronousRequest:request 
                        returningResponse:nil
                                    error:nil];
  
  NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  for (NSHTTPCookie *cookie in [cookieJar cookies]) {
    NSLog(@"%@", cookie);
  }

是不是得到了类似:

1
<NSHTTPCookie version:0 name:"PHPSESSID" value:"evf5rcboo8th1dnl53fs4ukmt2" expiresDate:(null) created:2012-03-13 14:28:13 +0000 (3.53342e+08) sessionOnly:TRUE domain:"blog.cnrainbird.com" path:"/" isSecure:FALSE>

的东东?这就是 cookie 啦

怎么清空 cookie 呢?

1
2
3
4
5
  NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];
  for (id obj in _tmpArray) {
    [cookieJar deleteCookie:obj];
  }

这样 cookie 就消失的一干二净了。

会查看 cookie 了,也会清空 cookie 了,怎么设置指定的 cookie 呢?

1
2
3
4
5
6
7
8
9
10
  NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
  [cookieProperties setObject:@"username" forKey:NSHTTPCookieName];
  [cookieProperties setObject:@"rainbird" forKey:NSHTTPCookieValue];
  [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieDomain];
  [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieOriginURL];
  [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
  [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
 
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

这样就可以了。输入一下,是不是得到了下面这样的结果:

1
<NSHTTPCookie version:0 name:"username" value:"rainbird" expiresDate:(null) created:2012-03-13 14:36:53 +0000 (3.53342e+08) sessionOnly:TRUE domain:"cnrainbird.com" path:"/" isSecure:FALSE>

其实 ios 的 cookie 就这么简单。如果 mac 也支持这么干的话,那以后写点登陆的脚本不是简单多了:)

Cookie 不会通过`Set-Cookie` Header 在 Chrome 中设置 我在前端 (Next.JS) 中通过 Express.JS 设置 cookie 时遇到了一些问题我找到了答案

我找到了答案

问题在于 corsfetch-api。

要设置 cookie,您需要在前端执行此操作:

fetch('<DOMAIN>/<PATH>',{
        ...
        credentials: 'include',mode: 'cors'
    }
)

在后端:

app.use(
    cors({
        credentials: true,origin: '<DOMAIN>'
    })
)

但您必须为后端提供完整的原始网址,例如:
您的前端localhost:3000上运行,您的后端在*localhost:8080**上运行。 这将是您的前端代码

fetch('http://localhost:8080/login',mode: 'cors'
        ...
    }
)

那么这就是后端代码

app.use(
    cors:({
        credentials: true,origin: 'http://localhost:3000'
    })
)

使用相同的方法,您可以将 Cookie 发送回您的 API。

Cookie 域问题:使用带有 jwt 令牌的 cookie 进行身份验证 - 无法为 cookie

如何解决Cookie 域问题:使用带有 jwt 令牌的 cookie 进行身份验证 - 无法为 cookie

假设我创建了一个名为鸡的平台。

我有 3 个应用程序(本地环境中的服务):

  • 前端应用:demo.chicken.test (vue.js)
  • api: demo.api.chicken.test (laravel)
  • socket 服务:ass.chicken.test(express 和 socket.io)

''demo'' 是客户的公司。因此,我可以拥有例如“快速”等其他平台可访问的平台。 所以我可以有 fast.chicken.test、fast.api.chicken.test 和 ass.chicken.test(ass 为所有客户端共享)

到目前为止,我发现了如何在 cookie 中存储 jwt 令牌。 前端应用程序在登录请求中发送凭据,并在响应中接收域为:demo.api.chicken.test 的 cookie。对 api 的每个请求都是愉快的浏览器附加带有令牌的 cookie。

但是我的问题出现了,当我想向 ass.chicken.test 发送握手请求时。 我无法使用来自 api 站点的域 ass.chicken.test 或 demo.chicken.test 设置 cookie,因为请求主机是 demo.api.chicken.test 并且浏览器阻止了它。

如果我使用 cookie 的域 chicken.test 那么它工作正常,但是 如果我在一个浏览器中打开两个客户端,然后登录第二个客户端覆盖此 cookie,一个客户端工作,第二个不工作。

我在这里发现 cookie 限制还是希望不将 jwt 令牌移动到本地存储?

cookie、vue-cookie、js-cookie

一、cookie是存储在客户端浏览器的一段文本信息

  1.大小限制在4KB

  2.一个网站的cookie数量在50条左右

  3.存入cookie有一定的风险

  4.可以设置有效期,默认的为会话结束

  5.一般路径为根目录

  6.可以设置域名

  7可以设置secure,通过https访问(secure属性:当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输,也就是只能在 HTTPS 连接中被浏览器传递到服务器端进行会话验证,如果是 HTTP 连接则不会传递该信息,所以不会被窃取到Cookie 的具体内容。)

cookie的完整格式

  document.cookie = ''name=vlaue;expires=日期对象;path=/;domain=域名;secure;

cookie的原生封装

 1 function createCookie(key,value,expires){
 2         var cookieText = encodeURIComponent(key) + ''='' + encodeURIComponent(value) + '';path=/'';
 3         if(!isNaN(expires)){
 4             var date = new Date();
 5             date.setDate(date.getDate() + expires);
 6             cookieText += '';expires='' + date;
 7         }
 8         document.cookie = cookieText;
 9     }
10     function getCookie(key){
11         var arr = document.cookie.split(''; '');
12         for(var i = 0,len = arr.length;i < len;i ++){
13             var list = arr[i].split(''='');
14             if(encodeURIComponent(key) === list[0]){
15                 return decodeURIComponent(list[1]);
16             }
17         }
18     }
19     function removeCookie(key){
20         document.cookie = encodeURIComponent(key) + ''=;path=/;expires='' + new Date(0);
21     }

 

二、vue-cookie

vue-cookie是在别人封装好的cookie,通过npm下载引入进行使用,npm地址:https://www.npmjs.com/package/vue-cookies,

npm下载到项目

npm i vue-cookies -D

下载到vue项目后,创建新的cookie.js文件在文件中写入

import Vue from ''Vue''
 import VueCookies from ''vue-cookies''
 Vue.use(VueCookies)

然后在main.js中引用

1 import ''./cookie.js''

这样就可以在各个组件中直接调用cookie

设置cookie

this.$cookies.set(keyName, value,time,path,domain,secure)//KeyName是cookie的键,value是对应的值,time为何时自动删除,path为存放路径,domain为域名,secure为是否要以安全的方式传输

如果是一个简单的项目,一般只会用到前四项

this.$cookies.set(''test'', ''{a:1,b:2}'',0,''/'')   //意思为创建一个名为 test 的cookie ,值为{a:1,b:2},时间为0,即会话结束就删除cookie,存放在根目录 / 下

vue-cookie的时间有以下几个格式

  1.直接设置数字:0,1,-1。设置为0即会话结束就删除cookie,设置1即一秒后删除,设置-1即此cookie永不删除,60 + 30即为1分30秒后,60*60即为1小时后,1就代表一秒

  2.设置为字符串,比如:"1s"即为一秒后删除、"2min"就是两分钟、"3h"就是3个小时、"4d"就是4天、"5m"就是5个月、"6y"就是6年

  3.也可以自定义设置时间 比如:new Date(2019,9,13).toUTCString()

 

1 this.$cookies.set(''test'', ''{a:1,b:2}'',-1)  //永不删除
 
3 this.$cookies.set(''test'', ''{a:1,b:2}'',60)  //一分钟后删除
 
5 this.$cookies.set(''test'', ''{a:1,b:2}'',''10s'')  //10秒后删除
 
7 this.$cookies.set(''test'', ''{a:1,b:2}'',new Date(2019,9,13).toUTCString())  //2019年10月13日删除,new Date月份设置是从0开始的

 

 

 

cookie的获取

1 this.$cookies.get(keyName)   //KeyName就是设置的cookie的名字,vue-cookie会将cookie自动转为json对象格式

cookie的删除

1 this.$cookies.remove(keyName)

支持判断本地是否有此cookie

1 this.$cookies.isKey(keyName)

获取本地所有cookie的名称

1 this.$cookies.keys()

 

三、js-cookie

js-cookie同样是npm中被人已经写好的cookie工具,npm地址:https://www.npmjs.com/package/js-cookie;

npm下载命令

1 cnpm i js-cookie -D

也可以在vue中使用,下面是在react中的使用

通过命令在react项目中下载成功后在main.js文件中引用

import jsCookie from ''js-cookie'';

React.Component.prototype.$cookie = jsCookie;  //添加到原型,使每一个组件都可以使用

js-cookie的使用就很简单

设置cookie

this.$cookie.set(''name'', ''value'');

设置删除时间

this.$cookie.set(''name'', ''value'',{expires: 7, path: ''/''});  //此时为7天后删除cookie,可以不设置,默认会话结束后自动删除cookie

获取cookie

this.$cookie.get(''name'');  //将会得到名为name的纯字符串值

this.$cookie.getJSON(''name'');   //将会得到一个json格式的对象数据

this.$cookie.get(); 

this.$cookie.getJSON();     //如果没有键名将会获取所有的cookie

删除cookie

1 this.$cookie.remove(''name'');

 

Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2

Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2

如何解决Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2

我正在尝试在 Delphi XE6 和 Indy (v10.6.2.0) 中发出 HTTPS Post 请求。

我能够成功调用此 API 并使用 JavaScript 获得响应:

JavaScript code causing API response to output twice

但我需要在 Delphi 应用程序中做同样的事情。

我尝试多次尝试使用 TIdSSLIOHandlerSocketopenSSL 的各种配置,但出现错误:

无法加载 SSL 库

libeay32.dll 是罪魁祸首。我还去了 Indy 的 GitHub 存储库,但不确定需要下载哪个依赖文件。

下面的代码给了我错误:

无法打开文件“C:\\Users..\\IndyHTTPSTest\\Win32\\Debug\\dXNlcm5hbWU6cGFzc3dvcmQ=”。系统找不到指定的文件。

要让它像 JavaScript 代码一样在 Delphi XE6 中运行,我缺少什么?

我已将 libeay32.dllssleay32.dll 包含在我的项目文件夹中。

这是我的代码:

  1. unit uMainForm;
  2. interface
  3. uses
  4. Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,IdBaseComponent,IdSSLOpenSSL,IdSSLOpenSSLHeaders,System.NetEncoding,IdComponent,IdTCPConnection,IdTCPClient,IdHTTP;
  5. type
  6. TMainForm = class(TForm)
  7. IdHTTP1: TIdHTTP;
  8. GetBtn: TButton;
  9. ResponseMemo: TMemo;
  10. procedure GetBtnClick(Sender: TObject);
  11. procedure FormCreate(Sender: TObject);
  12. private
  13. { Private declarations }
  14. public
  15. { Public declarations }
  16. end;
  17. var
  18. MainForm: TMainForm;
  19. ss: TStringStream;
  20. s,sLog,sFile : String;
  21. Base64: TBase64Encoding;
  22. Txt: TextFile;
  23. implementation
  24. {$R *.dfm}
  25. procedure TMainForm.FormCreate(Sender: TObject);
  26. begin
  27. // Assign JSON to TStringStream
  28. ss := TStringStream.Create(''locations= [{"address":"Test1","lat":"52.05429","lng":"4.248618"},{"address":"Test2","lat":"52.076892","lng":"4.26975"},{"address":"Test3","lat":"51.669946","lng":"5.61852"},{"address":"Sint-Oedenrode,The Netherlands","lat":"51.589548","lng":"5.432482"}]'',TEncoding.UTF8);
  29. // Base64 encode username and password string to satisfy API for "Authorization","Basic " + sLog);
  30. s := ''username:password'';
  31. Base64 := TBase64Encoding.Create(20,'''');
  32. sLog := Base64.Encode(s);
  33. end;
  34. procedure TMainForm.GetBtnClick(Sender: TObject);
  35. var
  36. responseFromServer,sWhichFail,sver :string;
  37. LHandler: TIdSSLIOHandlerSocketopenSSL;
  38. begin
  39. LHandler := TIdSSLIOHandlerSocketopenSSL.Create(self);
  40. with LHandler do
  41. begin
  42. SSLOptions.Method := sslvSSLv2;
  43. SSLOptions.Mode := sslmUnassigned;
  44. SSLOptions.VerifyMode := [];
  45. SSLOptions.VerifyDepth := 0;
  46. host := '''';
  47. end;
  48. IdHTTP1 := TIdHTTP.Create(Self);
  49. with IdHTTP1 do
  50. begin
  51. IOHandler := LHandler;
  52. AllowCookies := True;
  53. ProxyParams.BasicAuthentication := False;
  54. ProxyParams.ProxyPort := 0;
  55. Request.ContentLength := -1;
  56. Request.ContentType := ''application/x-www-form-urlencoded'';
  57. Request.ContentRangeEnd := 0;
  58. Request.ContentRangeStart := 0;
  59. Request.Accept := ''text/json,*/*'';
  60. Request.BasicAuthentication := True;
  61. Request.UserAgent := ''Mozilla/3.0 (compatible; Indy Library)'';
  62. HTTPOptions := [hoForceEncodeParams];
  63. end;
  64. try
  65. // Set up the request and send it
  66. sFile:= IdHTTP1.Post(''https://api.routexl.com/tour'',sLog);
  67. ResponseMemo.Lines.Add(Format(''Response Code: %d'',[IdHTTP1.ResponseCode]));
  68. ResponseMemo.Lines.Add(Format(''Response Text: %s'',[IdHTTP1.ResponseText]));
  69. finally
  70. sWhichFail:= WhichFailedToLoad();
  71. ShowMessage(sWhichFail);
  72. ResponseMemo.Lines.Text:= sWhichFail;
  73. //sver := OpenSSLVersion();
  74. //ShowMessage(sver);
  75. end;
  76. end;
  77. end.

更新:我更新了代码,并添加了屏幕截图以帮助调试:

  1. unit uMainForm;
  2. interface
  3. uses
  4. Winapi.Windows,IdHTTP;
  5. type
  6. TMainForm = class(TForm)
  7. IdHTTP1: TIdHTTP;
  8. GetBtn: TButton;
  9. ResponseMemo: TMemo;
  10. procedure GetBtnClick(Sender: TObject);
  11. procedure FormCreate(Sender: TObject);
  12. procedure FormDestroy(Sender: TObject);
  13. private
  14. { Private declarations }
  15. sl: TStringList;
  16. public
  17. { Public declarations }
  18. end;
  19. var
  20. MainForm: TMainForm;
  21. implementation
  22. {$R *.dfm}
  23. procedure TMainForm.FormCreate(Sender: TObject);
  24. begin
  25. sl := TStringList.Create;
  26. sl.Add(''locations=[{"address":"Test1","lng":"5.432482"}]'');
  27. end;
  28. procedure TMainForm.FormDestroy(Sender: TObject);
  29. begin
  30. sl.Free;
  31. end;
  32. procedure TMainForm.GetBtnClick(Sender: TObject);
  33. var
  34. LHTTP: TIdHTTP;
  35. LHandler: TIdSSLIOHandlerSocketopenSSL;
  36. responseFromServer,sver :string;
  37. begin
  38. ResponseMemo.Clear;
  39. LHTTP := TIdHTTP.Create(nil);
  40. try
  41. LHandler := TIdSSLIOHandlerSocketopenSSL.Create(LHTTP);
  42. LHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
  43. LHandler.SSLOptions.Mode := sslmClient;
  44. LHandler.SSLOptions.VerifyMode := [];
  45. LHandler.SSLOptions.VerifyDepth := 0;
  46. LHTTP.IOHandler := LHandler;
  47. LHTTP.AllowCookies := True;
  48. LHTTP.Request.Accept := ''text/json,*/*'';
  49. LHTTP.Request.BasicAuthentication := True;
  50. LHTTP.Request.ContentType := ''application/x-www-form-urlencoded'';
  51. LHTTP.Request.Username := ''username:'';
  52. LHTTP.Request.Password := ''password'';
  53. LHTTP.HTTPOptions := [hoForceEncodeParams];
  54. try
  55. responseFromServer := IdHTTP1.Post(''https://api.routexl.com/tour'',sl);
  56. ResponseMemo.Lines.Text := responseFromServer;
  57. except
  58. on E: EIdOSSLCouldNotLoadSsllibrary do
  59. begin
  60. sver := OpenSSLVersion();
  61. sWhichFail := WhichFailedToLoad();
  62. ResponseMemo.Lines.Add(sver);
  63. ResponseMemo.Lines.Add(sWhichFail);
  64. ShowMessage(''Could not load OpenSSL'');
  65. end;
  66. on E: EIdHTTPProtocolException do
  67. begin
  68. ResponseMemo.Lines.Add(Format(''Response Code: %d'',[LHTTP.ResponseCode]));
  69. ResponseMemo.Lines.Add(Format(''Response Text: %s'',[LHTTP.ResponseText]));
  70. ResponseMemo.Lines.Add(E.ErrorMessage);
  71. ShowMessage(''HTTP Failure'');
  72. end;
  73. on E: Exception do
  74. begin
  75. ResponseMemo.Lines.Add(E.ClassName);
  76. ResponseMemo.Lines.Add(E.Message);
  77. ShowMessage(''UnkNown Error'');
  78. end;
  79. end;
  80. finally
  81. LHTTP.Free;
  82. end;
  83. end;
  84. end.

WireShark 在执行 JavaScript 代码时显示以下内容:

image

WireShark 在执行 Delphi 代码时显示以下内容:

image

解决方法

当您从 Indy 收到 OpenSSL 加载错误时,您可以在 WhichFailedToLoad() 单元中调用 Indy 的 IdSSLOpenSSLHeaders 函数以找出原因。您是哪个,但您没有向我们展示它的实际含义,因此我们无法诊断 OpenSSL 无法加载的原因。通常,加载会失败,因为:

  • libeay32.dll 和/或 ssleay32.dll 找不到。如果它们与您的程序不在同一个文件夹中,或者至少不在操作系统的 DLL 搜索路径中,那么您可以在 IdOpenSSLSetLibPath() 单元中使用 Indy 的 IdSSLOpenSSLHeaders 函数来告诉 Indy 在哪里查找它们.

  • 它们与您的程序不同。 32 位程序无法加载 64 位 DLL,反之亦然。

  • 它们缺少必需的导出函数,这表明它们可能是 Indy 根本不支持的 OpenSSL 版本。例如,TIdSSLIOHandlerSocketOpenSSL 最多仅支持 OpenSSL 1.0.2u。对于 OpenSSL 1.1.x+,您需要改用 this experimental SSLIOHandler

话虽如此,我发现您的邮政编码有几个问题:

  • 您正在手动编码根本不需要手动编码的输入数据。 TIdHTTP.Post() 可以在内部为您处理。

  • 您正在调用以 TIdHTTP.Post() 文件名作为输入的 String 版本,它将打开指定的文件并按原样发送其内容。但是您不是传入文件名,而是传入 Base64 编码的用户凭据(甚至不是您的 JSON)。要发布正确的 application/x-www-webform-urlencoded 帖子,您需要使用 TIdHTTP.Post() 版本,该版本将 TStrings 对的 name=value 作为输入。

  • TIdHTTP 本机实现 Basic 身份验证,因此您应该使用 TIdHTTP.Request.UserNameTIdHTTP.Request.Password 属性作为您的用户凭据。

  • 您告诉 TIdSSLIOHandlerSocketOpenSSL 使用 SSL v2.0。没有人再使用它,因为它不再安全。您不应该使用低于 TLS v1.0 的任何东西作为绝对最小值。甚至在大多数服务器上也正在逐步淘汰。因此,请确保您还启用了 TLS v1.1 和 TLS v1.1(对于 TLS v1.3,您需要使用 other SSLIOHandler)。

坦率地说,您的 Delphi 代码甚至无法复制 Javascript 代码正在执行的操作。试试更像这样的东西:

  1. unit uMainForm;
  2. interface
  3. uses
  4. Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,IdBaseComponent,IdSSLOpenSSL,IdSSLOpenSSLHeaders,System.NetEncoding,IdComponent,IdTCPConnection,IdTCPClient,IdHTTP;
  5. type
  6. TMainForm = class(TForm)
  7. GetBtn: TButton;
  8. ResponseMemo: TMemo;
  9. procedure GetBtnClick(Sender: TObject);
  10. procedure FormCreate(Sender: TObject);
  11. procedure FormDestroy(Sender: TObject);
  12. private
  13. { Private declarations }
  14. sl: TStringList;
  15. public
  16. { Public declarations }
  17. end;
  18. var
  19. MainForm: TMainForm;
  20. implementation
  21. {$R *.dfm}
  22. procedure TMainForm.FormCreate(Sender: TObject);
  23. begin
  24. sl := TStringList.Create;
  25. sl.Add(''locations=[{"address":"Test1","lat":"52.05429","lng":"4.248618"},{"address":"Test2","lat":"52.076892","lng":"4.26975"},{"address":"Test3","lat":"51.669946","lng":"5.61852"},{"address":"Sint-Oedenrode,The Netherlands","lat":"51.589548","lng":"5.432482"}]'');
  26. end;
  27. procedure TMainForm.FormDestroy(Sender: TObject);
  28. begin
  29. sl.Free;
  30. end;
  31. procedure TMainForm.GetBtnClick(Sender: TObject);
  32. var
  33. LHTTP: TIdHTTP;
  34. LHandler: TIdSSLIOHandlerSocketOpenSSL;
  35. responseFromServer,sWhichFail,sVer :string;
  36. begin
  37. ResponseMemo.Clear;
  38. LHTTP := TIdHTTP.Create(nil);
  39. try
  40. LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(LHTTP);
  41. LHandler.SSLOptions.SSLVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
  42. LHandler.SSLOptions.Mode := sslmClient;
  43. LHandler.SSLOptions.VerifyMode := [];
  44. LHandler.SSLOptions.VerifyDepth := 0;
  45. LHTTP.IOHandler := LHandler;
  46. LHTTP.AllowCookies := True;
  47. LHTTP.Request.Accept := ''text/json,*/*'';
  48. LHTTP.Request.BasicAuthentication := True;
  49. LHTTP.Request.Username := ''username'';
  50. LHTTP.Request.Password := ''password'';
  51. LHTTP.HTTPOptions := [hoForceEncodeParams];
  52. try
  53. responseFromServer := IdHTTP1.Post(''https://api.routexl.com/tour'',sl);
  54. ResponseMemo.Lines.Text := responseFromServer;
  55. except
  56. on E: EIdOSSLCouldNotLoadSSLLibrary do
  57. begin
  58. sVer := OpenSSLVersion();
  59. sWhichFail := WhichFailedToLoad();
  60. ResponseMemo.Lines.Add(sVer);
  61. ResponseMemo.Lines.Add(sWhichFail);
  62. ShowMessage(''Could not load OpenSSL'');
  63. end;
  64. on E: EIdHTTPProtocolException do
  65. begin
  66. ResponseMemo.Lines.Add(Format(''Response Code: %d'',[LHTTP.ResponseCode]));
  67. ResponseMemo.Lines.Add(Format(''Response Text: %s'',[LHTTP.ResponseText]));
  68. ResponseMemo.Lines.Add(E.ErrorMessage);
  69. ShowMessage(''HTTP Failure'');
  70. end;
  71. on E: Exception do
  72. begin
  73. ResponseMemo.Lines.Add(E.ClassName);
  74. ResponseMemo.Lines.Add(E.Message);
  75. ShowMessage(''Unknown Error'');
  76. end;
  77. end;
  78. finally
  79. LHTTP.Free;
  80. end;
  81. end;
  82. end.

UPDATE:话虽如此,API documentation 显示了以下 curl 示例:

使用 cURL 获取未优化的路由:

  1. curl \\
  2. --url https://api.routexl.com/tour/ \\
  3. --user <username>:<password> \\
  4. --data ''locations=[{"address":"1",{"address":"2",{"address":"3",{"address":"4","lng":"5.432482"}]&skipOptimisation=true''

根据curl documentation,该示例确实会转换为上述 Delphi 代码:

-d,--data

(HTTP MQTT) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的那样。这将导致 curl 使用内容类型 application/x-www-form-urlencoded 将数据传递到服务器。与-F,--form相比。

除了一个细节 - 当发送启用了 application/x-www-form-urlencoded 选项的 hoForceEncodeParams 请求时,TIdHTTP.Post() 将对 []{} JSON 中的 、":, 字符,采用 %HH 格式。但是 GitHub 上的 API 的 Javascript and PHP examples 没有进行这种编码,他们按原样发送 JSON。因此,如果结果证明 API 服务器不喜欢该编码,则只需禁用 hoForceEncodeParams 选项:

  1. LHTTP.HTTPOptions := [];
,

您可以将 a,b,c = np.zeros((3,100)) 的声明更改为 sLog: String。然后在 sLog: TStrings 中实例化 onCreate,然后在代码 sLog := TStringList.Create 中实例化。

所以,

  1. sLog.Text := Base64.Encode(s);
  1. var
  2. MainForm: TMainForm;
  3. ss: TStringStream;
  4. s,sFile : String;
  5. sLog: TStrings;
  6. Base64: TBase64Encoding;
  7. Txt: TextFile;

关于IOS 中 http 请求使用 cookiehttp请求中的cookie的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Cookie 不会通过`Set-Cookie` Header 在 Chrome 中设置 我在前端 (Next.JS) 中通过 Express.JS 设置 cookie 时遇到了一些问题我找到了答案、Cookie 域问题:使用带有 jwt 令牌的 cookie 进行身份验证 - 无法为 cookie、cookie、vue-cookie、js-cookie、Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2的相关信息,请在本站寻找。

本文标签: