首页 > 解决方案 > HttpClient.GetAsync(requestUri) not fetching the full content from url

问题描述

After upgrading the version of Microsoft.NETCore.UniversalWindowsPlatform package in my UWP app from 5.0.0 to 6.2.9, HttpClient.GetAsync(requestUri) is not downloading the entire content from url.

I am using HttpClient.GetAsync() to get contents of the page and then extract some info from it. eg link. With 5.0.0 it was working as expected and the entire page content was downloaded. But after upgrading to 6.2.9, looks like only the schema is getting downloaded and not the content.

One thing I noticed is that the location of System.Net.Http.dll has changed between the versions:

This is the code I am using:

public static async void GetDataAsync(string url, Action<string> OnSuccess, Action<string> OnFailure)
{
    HttpClient _floatHttpClient = new HttpClient();
    string responseText = null;
    try
    {
        HttpResponseMessage response = await _floatHttpClient.GetAsync(url);

        response.EnsureSuccessStatusCode();
        responseText = await response.Content.ReadAsStringAsync();
        OnSuccess(responseText);
    }
    catch (Exception ex)
    {
        OnFailure(ex.ToString());
    }
}

How to download the full content from url with 6.2.9 package?

This is the caller method.

private void def_click(object sender, RoutedEventArgs e)
{
    DictDialog dictDialog = new DictDialog(Contentgrid.ActualWidth, Contentgrid.ActualHeight);
    dictDialog.ShowAsync();

    string url = "https://www.bing.com/search?q=+definition:" + dictWord + "&setmkt=en-us&setlang=en-us";

    WebRequest.GetDataAsync(url, (string html) =>
    {
        if (html != null)
        {
            bool defFound = false;
            string def = readerVM.GetDefinitionBing(html, out defFound);

            if (defFound == true)
            {
                dictDialog.LoadHtml(def);
            }
            else
            {
                dictDialog.ShowNoDefMsg();
            }
        }
        else
        {
            dictDialog.LoadFailed();
        }
    },
    (string error) =>
    {
        dictDialog.LoadFailed();
    });
}

标签: c#.net.net-coreuwphttpclient

解决方案


推荐阅读