https://nayapatrikadaily.com/news-article/2/News我正在嘗試使用 Http請求從新聞網(wǎng)站獲取 html 內(nèi)容Post。但是在響應中,頁面返回 Unicode 字符。我在將 Unicode 字符轉(zhuǎn)換為 html 時遇到了阻礙。網(wǎng)址:var nayapatrika = await ApiClient.PostAsync("https://nayapatrikadaily.com/ajax/pagination.php");異步后:public static async Task<HtmlDocument> PostAsync(string uri){ string responseJson = string.Empty; var htmlDocument = new HtmlDocument(); var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); var content = new MultipartFormDataContent(); var values = new[] { new KeyValuePair<string, string>("perPage", "20"), new KeyValuePair<string, string>("page", "2"), new KeyValuePair<string, string>("cat", "1"), }; foreach (var keyValuePair in values) { content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key); } var response = await client.PostAsync(uri, content); if (response.IsSuccessStatusCode) { responseJson = await response.Content.ReadAsStringAsync(); htmlDocument.LoadHtml(responseJson); } } return htmlDocument;}響應時,頁面始終返回以下字符。
1 回答

元芳怎么了
TA貢獻1798條經(jīng)驗 獲得超7個贊
反序列化 api 響應對我來說很有效。正如我注意到的,它有兩個屬性:newsList和numPages。
我創(chuàng)建了該類:ResponseObj
public class ResponseObj
{
public string numPage { get; set; }
public string newsList { get; set; }
}
并反序列化為ResponseObj
var obj = JsonConvert.DeserializeObject<ResponseObj>(responseJson);
var response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
responseJson = await response.Content.ReadAsStringAsync();
var obj = JsonConvert.DeserializeObject<ResponseObj>(responseJson);
htmlDocument.LoadHtml(obj.newsList);
}
- 1 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報
0/150
提交
取消