.net HttpClient post api 响应未正确呈现

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;

}

响应时,页面始终返回以下字符。

http://img.mukewang.com/64bb896c0001b15406500610.jpg

慕慕森
浏览 119回答 1
1回答

元芳怎么了

反序列化 api 响应对我来说很有效。正如我注意到的,它有两个属性:newsList和numPages。我创建了该类:ResponseObjpublic class ResponseObj{&nbsp; &nbsp; public string numPage { get; set; }&nbsp; &nbsp; public string newsList { get; set; }}&nbsp;并反序列化为ResponseObjvar obj = JsonConvert.DeserializeObject<ResponseObj>(responseJson);var response = await client.PostAsync(uri, content);if (response.IsSuccessStatusCode){&nbsp; &nbsp; responseJson = await response.Content.ReadAsStringAsync();&nbsp; &nbsp; var obj = JsonConvert.DeserializeObject<ResponseObj>(responseJson);&nbsp; &nbsp; htmlDocument.LoadHtml(obj.newsList);}
打开App,查看更多内容
随时随地看视频慕课网APP