gpt4 book ai didi

delphi - Indy header 上次修改 - 日期编码参数无效

转载 作者:行者123 更新时间:2023-12-03 15:57:36 29 4
gpt4 key购买 nike

我正在使用 Indy 的 TIdHTTP。我发出 2 个不同的请求,一个 header 包含“Last-Modified”标签,另一个 header 不包含。带有标签的 header 抛出异常:

'Invalid Argument to date encode'

我已经遇到过this问题在哪里Remy Lebeau说,TIdHttp 现在能够解析 ISO8601 日期,但它似乎对我不起作用。正如您在下面看到的,除了更改 UserAgent 之外,我没有对组件执行任何操作。我错过了什么吗?

url :=  'https://api.priceapi.com/v2/jobs/' + JobID+ '?token=' + Token;
http := TIdHTTP.Create(nil);
http.Request.UserAgent := 'XXXXX'; //Some UserAgent
try
ssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
http.IOHandler := ssl;
try
jo := TJsonObject.ParseJSONValue(http.get(url)) as TJSONObject;
result := jo.GetValue('status').Value;
finally
end;
finally
ssl.Free;
end;
finally
http.Free;
end;

带有上次修改时间的 header :

Cache-Control: no-cache
Content-Disposition: attachment;
filename="20181025145103_google_shopping_de_5bd1d857bbd7e520c12841d7.json"
Content-Transfer-Encoding: binary
Content-Type: application/json
Last-Modified: 2018-10-25 14:51:23 +0000
Vary: Origin
X-Accel-Buffering: no
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: b05aa8fe-7ea9-4152-8470-a75f9816549f
X-Runtime: 0.099212
X-XSS-Protection: 1; mode=block
transfer-encoding: chunked
Connection: keep-alive

没有上次修改时间的 header :

Cache-Control: max-age=0, private, must-revalidate', nil
Content-Type: application/json; charset=utf-8', nil
ETag: W/"43c4a8865a5ebe565f3920779a962e93"', nil
Vary: Origin', nil
X-Content-Type-Options: nosniff', nil
X-Frame-Options: SAMEORIGIN', nil
X-Request-Id: 344ac82e-0d14-4838-ae7e-627c79b78edc', nil
X-Runtime: 0.062357', nil
X-XSS-Protection: 1; mode=block', nil
Content-Length: 157', nil
Connection: Close', nil

堆栈跟踪:

:744717d2 KERNELBASE.RaiseException + 0x62
HIWBase.System.SysUtils.ConvertError($3B68860)
HIWBase.System.SysUtils.EncodeDate(???,???,???)
HIWBase.IdGlobalProtocols.RawStrInternetToDateTime('07:53:37 +0000',0)
HIWBase.IdGlobalProtocols.GMTToLocalDateTime('07:53:37 +0000')
HIWBase.IdHTTPHeaderInfo.TIdEntityHeaderInfo.ProcessHeaders
HIWBase.IdHTTPHeaderInfo.TIdResponseHeaderInfo.ProcessHeaders
HIWBase.IdHTTP.TIdHTTPProtocol.RetrieveHeaders(255)
HIWBase.IdHTTP.TIdCustomHTTP.DoRequest('GET','My URL',nil,$ADF09E0,(...))
HIWBase.IdHTTP.TIdCustomHTTP.Get('My URL',$ADF09E0,(...))
HIWBase.IdHTTP.TIdCustomHTTP.Get('My URL',(...))
HIWBase.IdHTTP.TIdCustomHTTP.Get('My URL')

我使用的是 Indy 版本 10.6.2.5311

最佳答案

Last-Modified header 定义于 RFC 2616 Section 14.29 1 为:

Last-Modified  = "Last-Modified" ":" HTTP-date

1: An equivalent definition appears in RFC 7232 Section 2.2.

HTTP-date is defined in RFC 2616 Section 3.3 2 as:

HTTP-date    = rfc1123-date | rfc850-date | asctime-daterfc1123-date = wkday "," SP date1 SP time SP "GMT"rfc850-date  = weekday "," SP date2 SP time SP "GMT"asctime-date = wkday SP date3 SP time SP 4DIGITdate1        = 2DIGIT SP month SP 4DIGIT               ; day month year (e.g., 02 Jun 1982)date2        = 2DIGIT "-" month "-" 2DIGIT               ; day-month-year (e.g., 02-Jun-82)date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))               ; month day (e.g., Jun  2)time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT               ; 00:00:00 - 23:59:59wkday        = "Mon" | "Tue" | "Wed"             | "Thu" | "Fri" | "Sat" | "Sun"weekday      = "Monday" | "Tuesday" | "Wednesday"             | "Thursday" | "Friday" | "Saturday" | "Sunday"month        = "Jan" | "Feb" | "Mar" | "Apr"             | "May" | "Jun" | "Jul" | "Aug"             | "Sep" | "Oct" | "Nov" | "Dec"

2: Equivalent definitions appear in RFC 7231 Section 7.1.1.1.

The Last-Modified value you have shown does not match any of those formats allowed by HTTP.

TIdHTTP uses Indy's GMTToLocalDateTime() function to parse the Last-Modified (and Date and Expires) header. That function is shared by HTTP, IMAP, NNTP, and email components, so it is a little more flexible in the date/time formats that it supports. For instance, it does parse ISO 8601, which you claim the Last-Modified value is. However, the value you have shown does not actually conform to ISO 8601, either. If it had, it would have looked more like this instead:

Last-Modified: 2018-10-26T08:37:01+00:00

更糟糕的是,根据您提供的堆栈跟踪,GMTToLocalDateTime()被调用时根本没有任何日期部分:

HIWBase.IdGlobalProtocols.GMTToLocalDateTime('07:53:37 +0000')

唯一可以发生在TIdHTTP的方式如果 HTTP 服务器正在发送 Last-Modified (或 DateExpires )具有该确切值的 header ,该值也不符合 HTTP 或 ISO 8601 标准,并且不会由 GMTToLocalDateTime() 按原样处理.

简而言之,您正在查询的 API 正在发送一个非法日期/时间格式TIdHTTP不支持解析(这很讽刺,因为主 https://www.priceapi.com 网站确实发送格式正确的 HTTP 日期/时间字符串)。您应该联系网站管理员并报告他们的 API 服务器在这方面违反了 HTTP 协议(protocol)标准。

话虽这么说,GMTToLocalDateTime()不提出 'Invalid Argument to date encode'当遇到格式错误的日期/时间字符串时出现异常。它返回 TDateTime0.0反而。您可能看到该异常的唯一方式是您在调试器内运行代码。当GMTToLocalDateTime()给定格式错误的日期/时间字符串,它可能会提取它认为有效的数字组件,但在尝试对最终 TDateTime 进行编码时会失败。跟他们。您看到的异常来自 RTL 的 EncodeDate()当给定无效的月/日/年作为输入时函数。但是GMTToLocalDateTime()在内部捕获该异常。您的代码在运行时永远不会看到它,只有调试器才能看到它。

关于delphi - Indy header 上次修改 - 日期编码参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003863/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com