修复腾讯财经接口GBK编码报错问题:注册编码提供程序,手动解析GBK响应

This commit is contained in:
claw_bot 2026-03-12 05:53:42 +00:00
parent 57e9c69a87
commit 4ce5e1b7f9

View File

@ -1,6 +1,7 @@
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using AssetManager.Data;
using AssetManager.Models.DTOs;
using Microsoft.Extensions.Logging;
@ -49,9 +50,13 @@ public class MarketDataService : IMarketDataService
{
_logger.LogInformation($"Requesting stock price for symbol: {symbol} (腾讯财经接口)");
// 注册GBK编码支持
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// 腾讯财经美股接口前缀us大写代码
var url = $"http://qt.gtimg.cn/q=us{symbol.ToUpper()}";
var response = await _httpClient.GetStringAsync(url);
var responseBytes = await _httpClient.GetByteArrayAsync(url);
var response = Encoding.GetEncoding("GBK").GetString(responseBytes);
if (string.IsNullOrEmpty(response) || !response.Contains("~"))
{