CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

BingV2Api for WinRT

787 Views   
/// <summary>Search Bing Web Index</summary> /// <see cref="http://www.bing.com/developers/s/APIBasics.html"/> /// <remarks>This service will be discontinued August 1, 2012</remarks> public class BingApiV2Helper { private string BingKey; public BingApiV2Helper(string bingKey) { this.BingKey = bingKey; } public async Task<List<Result>> SearchAsync(string query, SearchTypes type) { var _Template = "http://api.bing.net/json.aspx?Appid={0}&query={1}&sources={2}"; var _Url = string.Format(_Template, BingKey, query, type.ToString()); string _JsonString = string.Empty; try { // fetch from rest service var _HttpClient = new System.Net.Http.HttpClient(); var _HttpResponse = await _HttpClient.GetAsync(_Url); _JsonString = await _HttpResponse.Content.ReadAsStringAsync(); // check for error if (_JsonString.Contains("Parameter has invalid value") && _JsonString.Contains("AppId")) throw new InvalidCredentialsException { JSON = _JsonString }; // deserialize json to objects var _JsonBytes = Encoding.Unicode.GetBytes(_JsonString); using (MemoryStream _MemoryStream = new MemoryStream(_JsonBytes)) { var _JsonSerializer = new DataContractJsonSerializer(typeof(RootObject)); var _Result = (RootObject)_JsonSerializer.ReadObject(_MemoryStream); return _Result.SearchResponse.Phonebook.Results; } } catch (InvalidSomethingException) { throw; } catch (Exception e) { throw new InvalidSomethingException(e) { JSON = _JsonString }; } } public class InvalidSomethingException : Exception { public InvalidSomethingException() { } public InvalidSomethingException(Exception e) : base(string.Empty, e) { } public string JSON { get; set; } } public class InvalidCredentialsException : InvalidSomethingException { } public enum SearchTypes { /// <summary> /// Searches for web content /// </summary> Web, /// <summary> /// Searches for images on the web /// </summary> Image, /// <summary> /// Searches news stories /// </summary> News, /// <summary> /// Searches Encarta dictionary for spelling suggestions /// </summary> Spell, /// <summary> /// Searches phonebook entries /// </summary> Phonebook, /// <summary> /// Returns the query strings most similar to yours /// </summary> RelatedSearch, /// <summary> /// Searches for video on the web /// </summary> Video } [DataContract] public class Query { [DataMember] public string SearchTerms { get; set; } } [DataContract] public class Result { [DataMember] public string Title { get; set; } [DataMember] public string Url { get; set; } [DataMember] public string Business { get; set; } [DataMember] public string PhoneNumber { get; set; } [DataMember] public string Address { get; set; } [DataMember] public string City { get; set; } [DataMember] public string StateOrProvince { get; set; } [DataMember] public string CountryOrRegion { get; set; } [DataMember] public string PostalCode { get; set; } [DataMember] public double Latitude { get; set; } [DataMember] public double Longitude { get; set; } [DataMember] public string UniqueId { get; set; } [DataMember] public string DisplayUrl { get; set; } [DataMember] public double? UserRating { get; set; } [DataMember] public int? ReviewCount { get; set; } } [DataContract] public class Phonebook { [DataMember] public int Total { get; set; } [DataMember] public int Offset { get; set; } [DataMember] public string LocalSerpUrl { get; set; } [DataMember] public string Title { get; set; } [DataMember] public List<Result> Results { get; set; } } [DataContract] public class SearchResponse { [DataMember] public string Version { get; set; } [DataMember] public Query Query { get; set; } [DataMember] public Phonebook Phonebook { get; set; } } [DataContract] public class RootObject { [DataMember] public SearchResponse SearchResponse { get; set; } } }
by Jerry Nixon
  June 13, 2012 @ 8:31am
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies