The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP][C#]How to extract response of HttpClien? RRS feed

Answers

  • HTML Agility Pack can convert a html string to a formatted HTML structure, then you can query the div with id or ul with class..

    http://html-agility-pack.net/?z=codeplex

    • Proposed as answer by Barry Wang Friday, August 4, 2017 8:57 AM
    • Marked as answer by Nguyen_Thanh_Liem Friday, August 4, 2017 10:44 AM
    Thursday, July 27, 2017 9:36 AM

All replies

  • //Create an HTTP client object
    Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
    
    //Add a user-agent header to the GET request. 
    var headers = httpClient.DefaultRequestHeaders;
    
    //The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
    //especially if the header value is coming from user input.
    string header = "ie";
    if (!headers.UserAgent.TryParseAdd(header))
    {
        throw new Exception("Invalid header value: " + header);
    }
    
    header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
    if (!headers.UserAgent.TryParseAdd(header))
    {
        throw new Exception("Invalid header value: " + header);
    }
    
    Uri requestUri = new Uri("http://www.contoso.com");
    
    //Send the GET request asynchronously and retrieve the response as a string.
    Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
    string httpResponseBody = "";
    
    try
    {
        //Send the GET request
        httpResponse = await httpClient.GetAsync(requestUri);
        httpResponse.EnsureSuccessStatusCode();
        httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
    }
    catch (Exception ex)
    {
        httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
    }

    The code comes from the link you posted, httpReponseBody is a string type, not quite understand your question but maybe you will need this: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-search-strings-using-string-methods  

    Wednesday, July 26, 2017 3:16 AM
  • Ex: I want to find div with id or ul with class etc.
    Thursday, July 27, 2017 1:39 AM
  • HTML Agility Pack can convert a html string to a formatted HTML structure, then you can query the div with id or ul with class..

    http://html-agility-pack.net/?z=codeplex

    • Proposed as answer by Barry Wang Friday, August 4, 2017 8:57 AM
    • Marked as answer by Nguyen_Thanh_Liem Friday, August 4, 2017 10:44 AM
    Thursday, July 27, 2017 9:36 AM