C#: Bing-Search API
In this tutorial, let's talk about how to use Bing-Search API in C#.
Steps
1. Bing Account
First, create a Bing account at https://azure.microsoft.com/en-us/services/cognitive-services/search/ to use Bing services.
Bing services have 4 parts:
- Bing - Web search API,
- Bing - Image search API,
- Bing - Video search API,
- Bing - News search API
Bing - Web search API:
- q is a string where the data comes from textbox.
- count is total number of items.
- offset is a parameter that specifies the first entry to return from a collection.
- safeSearch is the safe search level of the results
https://cloud.githubusercontent.com/assets/11801842/16293084/2d466cea-3941-11e6-89e5-51ed3d84b97d.png
public async Task<string> GetDataByWebSearchAsync(string q, string count, int offset, string mkt, string safeSearch)
{
var client = new HttpClient();
var queryString = new Dictionary<string, string>();
//request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<Your key>");
//request parameters
queryString.Add("q", q);
queryString.Add("count", count);
queryString.Add("offset", offset.ToString());
queryString.Add("mkt", mkt);
queryString.Add("safesearch", safeSearch);
var uri = "https://bingapis.azure-api.net/api/v5/search/?"
+ "q=" + queryString["q"]
+ "&count=" + queryString["count"]
+ "&offset=" + queryString["offset"]
+ "&mkt=" + queryString["mkt"]
+ "&safesearch=" + queryString["safesearch"];
var response = await client.GetAsync(uri);
return await response.Content.ReadAsStringAsync();
}
News, Image, Video are the same as Web. Just replace the URI.
Bing - News search api
https://bingapis.azure-api.net/api/v5/news/search/?
Bing - Video search api
https://bingapis.azure-api.net/api/v5/videos/search/?&pricing=Free
Bing - Image search api
https://bingapis.azure-api.net/api/v5/images/search/?&license=Public