对于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)
- 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,可是想当然归想当然,真用的时候,真研究了一下发现还真强大。经过一番的研究简单说一下我的理解:当你访问一个网站时,不管你愿意或者不愿意,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 时遇到了一些问题我找到了答案
我找到了答案
问题在于 cors
和 fetch
-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 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.dll
和 ssleay32.dll
包含在我的项目文件夹中。
这是我的代码:
unit uMainForm;
interface
uses
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;
type
TMainForm = class(TForm)
IdHTTP1: TIdHTTP;
GetBtn: TButton;
ResponseMemo: TMemo;
procedure GetBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
ss: TStringStream;
s,sLog,sFile : String;
Base64: TBase64Encoding;
Txt: TextFile;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
// Assign JSON to TStringStream
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);
// Base64 encode username and password string to satisfy API for "Authorization","Basic " + sLog);
s := ''username:password'';
Base64 := TBase64Encoding.Create(20,'''');
sLog := Base64.Encode(s);
end;
procedure TMainForm.GetBtnClick(Sender: TObject);
var
responseFromServer,sWhichFail,sver :string;
LHandler: TIdSSLIOHandlerSocketopenSSL;
begin
LHandler := TIdSSLIOHandlerSocketopenSSL.Create(self);
with LHandler do
begin
SSLOptions.Method := sslvSSLv2;
SSLOptions.Mode := sslmUnassigned;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
host := '''';
end;
IdHTTP1 := TIdHTTP.Create(Self);
with IdHTTP1 do
begin
IOHandler := LHandler;
AllowCookies := True;
ProxyParams.BasicAuthentication := False;
ProxyParams.ProxyPort := 0;
Request.ContentLength := -1;
Request.ContentType := ''application/x-www-form-urlencoded'';
Request.ContentRangeEnd := 0;
Request.ContentRangeStart := 0;
Request.Accept := ''text/json,*/*'';
Request.BasicAuthentication := True;
Request.UserAgent := ''Mozilla/3.0 (compatible; Indy Library)'';
HTTPOptions := [hoForceEncodeParams];
end;
try
// Set up the request and send it
sFile:= IdHTTP1.Post(''https://api.routexl.com/tour'',sLog);
ResponseMemo.Lines.Add(Format(''Response Code: %d'',[IdHTTP1.ResponseCode]));
ResponseMemo.Lines.Add(Format(''Response Text: %s'',[IdHTTP1.ResponseText]));
finally
sWhichFail:= WhichFailedToLoad();
ShowMessage(sWhichFail);
ResponseMemo.Lines.Text:= sWhichFail;
//sver := OpenSSLVersion();
//ShowMessage(sver);
end;
end;
end.
更新:我更新了代码,并添加了屏幕截图以帮助调试:
unit uMainForm;
interface
uses
Winapi.Windows,IdHTTP;
type
TMainForm = class(TForm)
IdHTTP1: TIdHTTP;
GetBtn: TButton;
ResponseMemo: TMemo;
procedure GetBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
sl: TStringList;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
sl := TStringList.Create;
sl.Add(''locations=[{"address":"Test1","lng":"5.432482"}]'');
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
sl.Free;
end;
procedure TMainForm.GetBtnClick(Sender: TObject);
var
LHTTP: TIdHTTP;
LHandler: TIdSSLIOHandlerSocketopenSSL;
responseFromServer,sver :string;
begin
ResponseMemo.Clear;
LHTTP := TIdHTTP.Create(nil);
try
LHandler := TIdSSLIOHandlerSocketopenSSL.Create(LHTTP);
LHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
LHandler.SSLOptions.Mode := sslmClient;
LHandler.SSLOptions.VerifyMode := [];
LHandler.SSLOptions.VerifyDepth := 0;
LHTTP.IOHandler := LHandler;
LHTTP.AllowCookies := True;
LHTTP.Request.Accept := ''text/json,*/*'';
LHTTP.Request.BasicAuthentication := True;
LHTTP.Request.ContentType := ''application/x-www-form-urlencoded'';
LHTTP.Request.Username := ''username:'';
LHTTP.Request.Password := ''password'';
LHTTP.HTTPOptions := [hoForceEncodeParams];
try
responseFromServer := IdHTTP1.Post(''https://api.routexl.com/tour'',sl);
ResponseMemo.Lines.Text := responseFromServer;
except
on E: EIdOSSLCouldNotLoadSsllibrary do
begin
sver := OpenSSLVersion();
sWhichFail := WhichFailedToLoad();
ResponseMemo.Lines.Add(sver);
ResponseMemo.Lines.Add(sWhichFail);
ShowMessage(''Could not load OpenSSL'');
end;
on E: EIdHTTPProtocolException do
begin
ResponseMemo.Lines.Add(Format(''Response Code: %d'',[LHTTP.ResponseCode]));
ResponseMemo.Lines.Add(Format(''Response Text: %s'',[LHTTP.ResponseText]));
ResponseMemo.Lines.Add(E.ErrorMessage);
ShowMessage(''HTTP Failure'');
end;
on E: Exception do
begin
ResponseMemo.Lines.Add(E.ClassName);
ResponseMemo.Lines.Add(E.Message);
ShowMessage(''UnkNown Error'');
end;
end;
finally
LHTTP.Free;
end;
end;
end.
WireShark 在执行 JavaScript 代码时显示以下内容:
WireShark 在执行 Delphi 代码时显示以下内容:
解决方法
当您从 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 experimentalSSLIOHandler
。
话虽如此,我发现您的邮政编码有几个问题:
-
您正在手动编码根本不需要手动编码的输入数据。
TIdHTTP.Post()
可以在内部为您处理。 -
您正在调用以
TIdHTTP.Post()
文件名作为输入的String
版本,它将打开指定的文件并按原样发送其内容。但是您不是传入文件名,而是传入 Base64 编码的用户凭据(甚至不是您的 JSON)。要发布正确的application/x-www-webform-urlencoded
帖子,您需要使用TIdHTTP.Post()
版本,该版本将TStrings
对的name=value
作为输入。 -
TIdHTTP
本机实现Basic
身份验证,因此您应该使用TIdHTTP.Request.UserName
和TIdHTTP.Request.Password
属性作为您的用户凭据。 -
您告诉
TIdSSLIOHandlerSocketOpenSSL
使用 SSL v2.0。没有人再使用它,因为它不再安全。您不应该使用低于 TLS v1.0 的任何东西作为绝对最小值。甚至在大多数服务器上也正在逐步淘汰。因此,请确保您还启用了 TLS v1.1 和 TLS v1.1(对于 TLS v1.3,您需要使用 otherSSLIOHandler
)。
坦率地说,您的 Delphi 代码甚至无法复制 Javascript 代码正在执行的操作。试试更像这样的东西:
unit uMainForm;
interface
uses
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;
type
TMainForm = class(TForm)
GetBtn: TButton;
ResponseMemo: TMemo;
procedure GetBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
sl: TStringList;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
sl := TStringList.Create;
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"}]'');
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
sl.Free;
end;
procedure TMainForm.GetBtnClick(Sender: TObject);
var
LHTTP: TIdHTTP;
LHandler: TIdSSLIOHandlerSocketOpenSSL;
responseFromServer,sWhichFail,sVer :string;
begin
ResponseMemo.Clear;
LHTTP := TIdHTTP.Create(nil);
try
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(LHTTP);
LHandler.SSLOptions.SSLVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
LHandler.SSLOptions.Mode := sslmClient;
LHandler.SSLOptions.VerifyMode := [];
LHandler.SSLOptions.VerifyDepth := 0;
LHTTP.IOHandler := LHandler;
LHTTP.AllowCookies := True;
LHTTP.Request.Accept := ''text/json,*/*'';
LHTTP.Request.BasicAuthentication := True;
LHTTP.Request.Username := ''username'';
LHTTP.Request.Password := ''password'';
LHTTP.HTTPOptions := [hoForceEncodeParams];
try
responseFromServer := IdHTTP1.Post(''https://api.routexl.com/tour'',sl);
ResponseMemo.Lines.Text := responseFromServer;
except
on E: EIdOSSLCouldNotLoadSSLLibrary do
begin
sVer := OpenSSLVersion();
sWhichFail := WhichFailedToLoad();
ResponseMemo.Lines.Add(sVer);
ResponseMemo.Lines.Add(sWhichFail);
ShowMessage(''Could not load OpenSSL'');
end;
on E: EIdHTTPProtocolException do
begin
ResponseMemo.Lines.Add(Format(''Response Code: %d'',[LHTTP.ResponseCode]));
ResponseMemo.Lines.Add(Format(''Response Text: %s'',[LHTTP.ResponseText]));
ResponseMemo.Lines.Add(E.ErrorMessage);
ShowMessage(''HTTP Failure'');
end;
on E: Exception do
begin
ResponseMemo.Lines.Add(E.ClassName);
ResponseMemo.Lines.Add(E.Message);
ShowMessage(''Unknown Error'');
end;
end;
finally
LHTTP.Free;
end;
end;
end.
UPDATE:话虽如此,API documentation 显示了以下 curl 示例:
使用 cURL 获取未优化的路由:
curl \\
--url https://api.routexl.com/tour/ \\
--user <username>:<password> \\
--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
选项:
,
LHTTP.HTTPOptions := [];
您可以将 a,b,c = np.zeros((3,100))
的声明更改为 sLog: String
。然后在 sLog: TStrings
中实例化 onCreate
,然后在代码 sLog := TStringList.Create
中实例化。
所以,
sLog.Text := Base64.Encode(s);
var
MainForm: TMainForm;
ss: TStringStream;
s,sFile : String;
sLog: TStrings;
Base64: TBase64Encoding;
Txt: TextFile;
关于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的相关信息,请在本站寻找。
本文标签: