You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Chrome复制的PowerShell命令调用API时DateTime格式错误排查

问题:执行Chrome复制的Invoke-WebRequest命令时出现DateTime格式错误

问题详情

从Chrome开发者工具网络面板复制「Copy as PowerShell」生成的命令,粘贴到PowerShell执行时,报错提示字符串未被识别为有效的DateTime。不确定是哪个日期相关请求头导致的问题,比如Expires或If-Modified-Since。

复制的命令如下:

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
$session.Cookies.Add((New-Object System.Net.Cookie("ApplicationGatewayAffinityCORS", "c00ac4926aa1674224fe8bf89fd800cc", "/", "example.com")))
$session.Cookies.Add((New-Object System.Net.Cookie("ApplicationGatewayAffinity", "c00ac4926aa1674224fe8bf89fd800cc", "/", "example.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/api/v1/data" `
-WebSession $session `
-Headers @{
"Accept"="application/json, text/plain, */*"
  "Accept-Encoding"="gzip, deflate, br"
  "Accept-Language"="en-US,en;q=0.9"
  "Cache-Control"="no-cache"
  "Expires"="Sat, 01 Jan 2000 00:00:00 GMT"
  "If-Modified-Since"="0"
  "Pragma"="no-cache"
  "Referer"="https://example.com/"
  "Sec-Fetch-Dest"="empty"
  "Sec-Fetch-Mode"="cors"
  "Sec-Fetch-Site"="same-site"
  "X-Authentication"="ey...HZGcc0Q"
  "X-Correlation-Id"="7952caaa-ea81-47c1-aef9-79097ac8592e"
  "X-Request-Id"="0bd22d24-1b4c-407d-ba7c-38e0c7eb089f"
  "sec-ch-ua"="`"Chromium`";v=`"106`", `"Google Chrome`";v=`"106`", `"Not;A=Brand`";v=`"99`""
  "sec-ch-ua-mobile"="?0"
  "sec-ch-ua-platform"="`"Windows`""
}

报错信息:

Invoke-WebRequest : String was not recognized as a valid DateTime.
At line:1 char:1
+ Invoke-WebRequest -UseBasicParsing -Uri "https://example.com ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

解决方案

问题根源是If-Modified-Since头的取值"0"。PowerShell的Invoke-WebRequest会自动尝试将日期类请求头解析为DateTime对象,而"0"不属于有效的HTTP日期格式。Chrome复制的这个值是浏览器用来强制跳过缓存的特殊写法,但PowerShell不支持。

有两种解决方式:

  1. 直接移除If-Modified-Since头:已有Cache-Control: no-cache和Pragma: no-cache足够控制缓存,无需额外保留该头。修改后的命令如下:
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
$session.Cookies.Add((New-Object System.Net.Cookie("ApplicationGatewayAffinityCORS", "c00ac4926aa1674224fe8bf89fd800cc", "/", "example.com")))
$session.Cookies.Add((New-Object System.Net.Cookie("ApplicationGatewayAffinity", "c00ac4926aa1674224fe8bf89fd800cc", "/", "example.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/api/v1/data" `
-WebSession $session `
-Headers @{
"Accept"="application/json, text/plain, */*"
  "Accept-Encoding"="gzip, deflate, br"
  "Accept-Language"="en-US,en;q=0.9"
  "Cache-Control"="no-cache"
  "Expires"="Sat, 01 Jan 2000 00:00:00 GMT"
  "Pragma"="no-cache"
  "Referer"="https://example.com/"
  "Sec-Fetch-Dest"="empty"
  "Sec-Fetch-Mode"="cors"
  "Sec-Fetch-Site"="same-site"
  "X-Authentication"="ey...HZGcc0Q"
  "X-Correlation-Id"="7952caaa-ea81-47c1-aef9-79097ac8592e"
  "X-Request-Id"="0bd22d24-1b4c-407d-ba7c-38e0c7eb089f"
  "sec-ch-ua"="`"Chromium`";v=`"106`", `"Google Chrome`";v=`"106`", `"Not;A=Brand`";v=`"99`""
  "sec-ch-ua-mobile"="?0"
  "sec-ch-ua-platform"="`"Windows`""
}
  1. 修改为有效HTTP日期格式:如果必须保留该头,可设置为一个过去的GMT日期,比如和Expires取值一致:
"If-Modified-Since"="Sat, 01 Jan 2000 00:00:00 GMT"

内容的提问来源于stack exchange,提问作者sean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:15:36