0
votes

I am calling www.boardgamegeek.com API.

I am using below code:

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://www.boardgamegeek.com/xmlapi/collection/dhasmain");
    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/xml"));

    // List data response.
    HttpResponseMessage response = client.GetAsync("?own=1").Result; 
    if (response.IsSuccessStatusCode)
    {
        var dataObjects = response.Content.ReadAsStringAsync().Result;
    }
    else
    {
        Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
    }

But it is showing Result=Not yet computed.

Can anyone please suggest to me what the issue is?

I am also using the below code but it is not returning anything.

string dataObjects = ""; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://www.boardgamegeek.com/xmlapi/collection/zefquaavius"); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/xml"));

    client.GetStringAsync("?own=1").ContinueWith(task =>
    {
        dataObjects = task.Result;
    });
1

1 Answers

0
votes

It appears that you are calling the get asynchronously and not giving the call enough time for the call to complete when you access the result. You can use ContinueWith to respond to the call once the request is completed:

client.GetStringAsync("?own=1").ContinueWith(task =>
    {
        string dataObjects = task.Result;
    });

EDIT: Based on your comments, I see your UTF-8 encoding issue. The code below will handle the encoding issue so you have a string of XML from the response.

byte[] dataObjects = null;
client.GetByteArrayAsync("?own=1").ContinueWith(task => { dataObjects = task.Result; }).Wait();
string xmlResponse = System.Text.Encoding.UTF8.GetString(dataObjects);