In this article, I will show how to generate Bing Search subscription key and integrate into the Xamarin application.
Register Bing Search in Azure Portal:
You need to create an Azure account and generateStep 1:
Step 3:
Step 4:
Create Bing Web Search Xamarin Application:
Let's start with creating a new Xamarin Forms Project using Visual Studio. Open Run >> Type “DevenevIt will automatically create multiple projects, like .NET Standard, Android, iOS, and UWP.
Install Newtonsoft. Json :
Bing Web Search API will return Json object value so make sure you have added the Newtonsoft JSON NuGet Package to your all project. Right Click on Solution > Manage Nuget Package > Install Newtonsoft Json
Install Microsoft. Csharp :
This Design View:
After successfully install above two<
<
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinBingWebSearch"
:Class="XamarinBingWebSearch.MainPage">
<
<
<Label Text="Search" ></Label>
<Entry x
<ListView x:Name="lstwebsearch" BackgroundColor="Azure">
<
<
<
<
<Label Text="{Binding Snippet}">
</Label>
<Label Text="{Binding DisplayUrl}">
</Label>
<Label Text="{Binding Name}" TextColor="Gray"></Label>
<Label Text="{Binding Url}" TextColor="Gray"></Label>
</
</
</
</
</
</
</
</
{
public class WebSearch
{
public string Id { get; set; }
public string Name { get; set; }
public string Snippet { get; set; }
public string DisplayUrl { get; set; }
}
}
Configure the project:
We need to get web search result so send a GET request to the following endpoint and replace subscription key from Mainpage.xaml.csprivate string WebSearchEndPoint = "https://api.cognitive.microsoft.com/bing/v7.0/search?";
{
}
{
InitializeComponent
WebSearchClient = new HttpClient();
WebSearchClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "YOUR API KEY");
}
Get and Parse Json Data:
{
{
if (entrysearch != null)
lstwebsearch.ItemsSource = await SearchBingWeb(entrysearch.Text);
}
catch (Exception)
{
}
}
async Task<IEnumerable<WebSearch>> SearchBingWeb(string searchText)
{
List<WebSearch> websearch = new List<WebSearch>();
string count = "10";
string offset = "0";
string mkt = "en-us";
var result = await RequestAndAutoRetryWhenThrottled(() => WebSearchClient.GetAsync(string.Format("{0}q={1}&count={2}&offset={3}&mkt={4}", WebSearchEndPoint, WebUtility.UrlEncode(searchText), count, offset, mkt)));
for (int i = 0; i < 10; i++)
{
websearch.Add(new WebSearch
{
Id = data.webPages.value[i].id,
Name = data.webPages.value[i].name,
Url = data.webPages.value[i].url,
Snippet = data.webPages.value[i].snippet,
DisplayUrl = data.webPages.value[i].displayUrl
});
}
return websearch;
}
private async Task<HttpResponseMessage> RequestAndAutoRetryWhenThrottled(Func<Task<HttpResponseMessage>> action)
{
int retriesLeft = 6;
int delay = 500;
HttpResponseMessage response = null;
while (true)
{
response = await action();
if ((int)response.StatusCode == 429 && retriesLeft > 0)
{
await Task.Delay(delay);
retriesLeft--;
delay *= 2;
continue;
}
else
{
break;
}
}
return response;
}
Run the Application:
We have completed the coding now startYou can find the source code at C# Corner attachment and Github XamarinBingWebSearch repository as Microsoft-Cognitive-Service.
I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting artificial intelligence online training
ReplyDelete