https://nayapatrikadaily.com/news-article/2/News我正在尝试使用 Http请求从新闻网站获取 html 内容Post。但是在响应中,页面返回 Unicode 字符。我在将 Unicode 字符转换为 html 时遇到了阻碍。网址: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条经验 获得超7个赞
反序列化 api 响应对我来说很有效。正如我注意到的,它有两个属性:newsList和numPages。
我创建了该类: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 关注
- 118 浏览
添加回答
举报
0/150
提交
取消