.net HttpClient发布API响应无法正确呈现

时间:2019-07-27 18:38:18

标签: c# html-agility-pack asp.net-core-2.1

我正在尝试从新闻网站https://nayapatrikadaily.com/news-article/2/News获取html内容, Http Post请求。

但是,在响应中,页面正在返回Unicode字符。

在将Unicode字符转换为html时,我受阻。

URL

var nayapatrika = await ApiClient.PostAsync("https://nayapatrikadaily.com/ajax/pagination.php");

PostAsync:

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;
}

响应时,页面总是返回以下字符。

enter image description here

1 个答案:

答案 0 :(得分:0)

反序列化api响应对我有用。我注意到响应有两个属性:newsListnumPages

我创建了课程: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);
}