0

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 Answer 1

0

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); 
Sign up to request clarification or add additional context in comments.

5 Comments

I am now using the following code but it is not returning anything. string dataObjects = ""; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("boardgamegeek.com/xmlapi/collection/zefquaavius"); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/xml")); client.GetStringAsync("?own=1").ContinueWith(task => { dataObjects = task.Result; });
If it's a simple app where you don't mind blocking the main thread you are on (and thus just want to run synchronously, you can add a Wait at the end, so client.GetStringAsync("?own=1").ContinueWith(task => { dataObjects = task.Result; }).Wait();. Then you know dataObjects will have the value after the Wait is called.
When I am calling the client.GetStringAsync("?own=1").ContinueWith(task => { dataObjects = task.Result; }).Wait(); It is giving the exception "system.aggregateexception was unhandled by user code"
When I am calling the client.GetStringAsync("?own=1").ContinueWith(task => { dataObjects = task.Result; }).Wait(); It is giving the following error '"UTF-8"' is not a supported encoding name. Parameter name: name
Just posted edit that addresses the encoding issue. Hope that helps!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.