Powershell Invoke-WebRequest和字符编码 [英] Powershell Invoke-WebRequest and character encoding

查看:398
本文介绍了Powershell Invoke-WebRequest和字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过他们的Web API从Spotify数据库中获取信息. 但是,我遇到重音元音(ä,ö,ü等)的问题

I am trying to get information from the Spotify database through their Web API. However, I'm facing issues with accented vowels (ä,ö,ü etc.)

让我们以蒂斯托为例. Spotify的API浏览器可以正确显示信息: https://developer.spotify.com/web-api /console/get-artist/?id = 2o5jDhtHVPhrJdv3cEQ99Z

Lets take Tiësto as an example. Spotify's API Browser can display the information correctly: https://developer.spotify.com/web-api/console/get-artist/?id=2o5jDhtHVPhrJdv3cEQ99Z

如果我使用Invoke-Webrequest进行API调用,则会得到

If I make a API call with Invoke-Webrequest I get

Ti ?? sto

Ti??sto

作为名称:

function Get-Artist {
param($ArtistID = '2o5jDhtHVPhrJdv3cEQ99Z',
      $AccessToken = 'MyAccessToken')


$URI = "https://api.spotify.com/v1/artists/{0}" -f $ArtistID

$JSON = Invoke-WebRequest -Uri $URI -Headers @{"Authorization"= ('Bearer  ' + $AccessToken)} 
$JSON = $JSON | ConvertFrom-Json
return $JSON
}

如何获得正确的名字?

推荐答案

Jeroen Mostert ,在评论中问题,很好地说明了问题:

Jeroen Mostert, in a comment on the question, explains the problem well:

问题在于,Spotify(不明智地)没有返回其标头中使用的编码. PowerShell假定ISO-8859-1遵守该标准,但不幸的是,该站点正在使用UTF-8 . (PowerShell应该在这里忽略标准并采用UTF-8,但这就像我的观点一样.)更多详细信息

The problem is that Spotify is (unwisely) not returning the encoding it's using in its headers. PowerShell obeys the standard by assuming ISO-8859-1, but unfortunately the site is using UTF-8. (PowerShell ought to ignore standards here and assume UTF-8, but that's just like, my opinion, man.) More details here, along with the follow-up ticket.

不需要使用临时文件的解决方法重新编码不正确读取的字符串.

A workaround that doesn't require the use of temporary files is to re-encode the incorrectly read string.

如果我们假设存在函数convertFrom-MisinterpretedUtf8,则可以使用以下代码:

If we assume the presence of a function convertFrom-MisinterpretedUtf8,we can use the following:

$JSON = convertFrom-MisinterpretedUtf8 (Invoke-WebRequest -Uri $URI ...)

有关功能的定义,请参见下文.

See below for the function's definition.

function convertFrom-MisinterpretedUtf8([string] $String) {
  [System.Text.Encoding]::UTF8.GetString(
     [System.Text.Encoding]::GetEncoding(28591).GetBytes($String)
  )
}

该函数根据错误应用的编码(ISO-8859-1)将错误读取的字符串转换回字节,然后根据实际编码(UTF-8)重新创建字符串.

The function converts the incorrectly read string back to bytes based on the mistakenly applied encoding (ISO-8859-1) and then recreates the string based on the actual encoding (UTF-8).

这篇关于Powershell Invoke-WebRequest和字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆