如何在PowerShell中用Invoke-WebRequest向ShoutCast v2发送歌曲元数据
解决建议
针对你用PowerShell的Invoke-WebRequest向ShoutCast v2发送元数据报错的问题,可尝试以下几种方案:
1. 分离身份验证信息,避免URL嵌入凭证
Invoke-WebRequest对URL中直接嵌入用户名密码的处理逻辑和curl不同,建议单独通过-Credential参数传递身份信息:
# 创建凭证对象 $credential = New-Object System.Management.Automation.PSCredential ( $SC_server_username, (ConvertTo-SecureString $SC_server_password -AsPlainText -Force) ) # 构造不含凭证的URL $url = "http://${SC_ip_address}:${SC_port}/admin.cgi?${SC_stream_password}&mode=updinfo&song=${song}" # 执行请求 Invoke-WebRequest -Uri $url -Credential $credential
2. 对歌曲名称进行URL编码
如果song变量包含空格、特殊字符,curl会自动编码,但Invoke-WebRequest不会,需手动处理:
$encodedSong = [Uri]::EscapeDataString($song) $url = "http://${SC_ip_address}:${SC_port}/admin.cgi?${SC_stream_password}&mode=updinfo&song=${encodedSong}" # 结合凭证参数执行 Invoke-WebRequest -Uri $url -Credential $credential
3. 启用基础解析模式
部分环境下默认的IE解析引擎可能导致异常,添加-UseBasicParsing参数绕过:
Invoke-WebRequest -Uri $url -Credential $credential -UseBasicParsing
4. 指定TLS版本
如果ShoutCast服务器要求特定TLS版本,强制设置后再执行请求:
# 设置为TLS 1.2,可根据服务器支持情况调整 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri $url -Credential $credential
内容的提问来源于stack exchange,提问作者GavinD
相关产品推荐
相关产品推荐

