【问题标题】:Encoding for international character国际字符编码
【发布时间】:2016-08-18 09:24:34
【问题描述】:

我正在制作一个 Google 建议 API

我正在请求“¿cómo estás”这个词应该返回的地方

¿cómo estás ¿cómo estás hoy ¿cómo estás 意思 ¿cómo estás tú ¿cómo estás cuando nadas ¿cómo estás yo (1) ¿cómo estás yo ¿cómo estás response ¿cómo estás in English

但它正在显示

[{"短语":"como estás"},{"短语":"cómo estás en inglés"},{"短语":"como estás em espanhol"},{"短语":"cómo estás lleva acento"},{"短语":"cómo estás tú"},{"短语":"cómo estás hola"},{"短语":"como estas 的意思"},{"短语":"como estas 回复"},{"Phrase":"como estas usted"},{"Phrase":"como estas hoy"}]

反应是

忽略关键字。

你可以看到

¿

结果中缺少

我的代码是

System.Net.HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
request.ContentType = "text/html; charset=utf-8";
using (System.Net.HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("ISO-8859-2"));
    result = reader.ReadToEnd();
}

XDocument doc = XDocument.Parse(result);

if (!ReferenceEquals(doc.Descendants("suggestion"), null) && doc.Descendants("suggestion").Count() > 0)
{
    var suggestions = from suggestion in doc.Descendants("CompleteSuggestion") select new GoogleSuggestion { Phrase = suggestion.Element("suggestion").Attribute("data").Value };
    allSuggestions = suggestions.ToList();
}

请告知应该使用哪种编码...

【问题讨论】:

  • 响应头说的编码是什么?我希望它是 UTF-8、UTF-16 或 UTF-32,因为它们是 RFC 7159 支持的唯一编码。 UTF-8 似乎最有可能。当然,也有可能是您发送的请求存在问题,需要对相同的字符进行编码——您没有向我们展示这一点。
  • @JonSkeet,感觉在天空,引起了你的注意,我已经更新了代码库,并以图像的形式响应......
  • 嗯...这是声称它是 XML。您确定这是实际包含 JSON 的响应吗? (顺便说一句,我根本不知道这是否是受支持的 API。如果是 Autosuggest API,我认为它已被撤回。)
  • 我在 json 中手动转换它.. 虽然当我在转换之前检查响应时缺少“¿”字符
  • “我正在手动将其转换为 JSON”是什么意思?您的意思是实际响应 is 在 XML 中,并且您刚刚向我们展示了您的转换结果?这在问题中根本不清楚......当您应该向我们展示 XML 时,向我们展示 JSON 似乎毫无意义。

标签: c# unicode character-encoding system.net.httpwebrequest


【解决方案1】:

如果有人能得到帮助..

我通过使用解决了它

 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("ISO-8859-1"));

【讨论】:

  • 请接受您的回答,以便其他用户知道该问题已得到满意回答。
猜你喜欢
  • 1970-01-01
  • 2012-07-24
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多