I'd like to create a simple, async request to Google search.
According to Google, the simplest way to do this is using their JSON API with the simple curl request
curl -e http://www.my-ajax-site.com \ 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton'
I'd like to pull the first 5 pages of results and add the URL's of each result to an array. I find it unbelievably difficult to find any well-explained tutorials on HttpClient.GetAsync. I haven't got any further than this:
public String[] search(String term = "")
{
var rq = new HttpClient();
var uri = new Uri("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:" + term);
rq.GetAsync(uri);
}
I suppose this should start a task so I won't block the main thread, but how do I register a callback method for when the request is completed?
Since the GetAsync is a task you can do
rq.GetAsync(uri).ContinueWith((requestTask) => SomeMethod(requestTask););
HttpResponseMessage response = await rq.GetAsync(uri);
//put here your continuation logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With