You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cognitive-services/Bing-Video-Search/quickstarts/csharp.md
+22-21Lines changed: 22 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,26 +9,26 @@ manager: nitinme
9
9
ms.service: cognitive-services
10
10
ms.subservice: bing-video-search
11
11
ms.topic: quickstart
12
-
ms.date: 12/09/2019
12
+
ms.date: 05/22/2020
13
13
ms.author: aahi
14
14
---
15
15
16
16
# Quickstart: Search for videos using the Bing Video Search REST API and C#
17
17
18
-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple C# application sends an HTTP video search query to the API, and displays the response. While this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
18
+
Use this quickstart to make your first call to the Bing Video Search API. This simple C# application sends an HTTP video search query to the API, and displays the JSON response. Although this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
19
19
20
20
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/dotnet/Search/BingVideoSearchv7.cs) with additional error handling, features, and code annotations.
21
21
22
22
## Prerequisites
23
23
* Any edition of [Visual Studio 2017 or later](https://www.visualstudio.com/downloads/).
24
24
* The [Json.NET](https://www.newtonsoft.com/json) framework, available as a NuGet package.
25
-
* If you are using Linux/MacOS, this application can be run using [Mono](https://www.mono-project.com/).
25
+
* If you're using Linux/MacOS, you can run this application by using [Mono](https://www.mono-project.com/).
1. Create a new console solution in Visual Studio. Then add the following namespaces into the main code file.
31
+
1. Create a new console solution in Visual Studio. Then, add the following namespaces to the main code file:
32
32
33
33
```csharp
34
34
using System;
@@ -38,41 +38,42 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
38
38
using System.Collections.Generic;
39
39
```
40
40
41
-
2. Add variables for your subscription key, endpoint, and search term. `uriBase` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
41
+
2. Add variables for your subscription key, endpoint, and search term. For the `uriBase` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
### Create a struct to format the Bing Video Search API response
49
+
## Create a struct to format the Bing Video Search API response
50
50
51
-
1. Define a `SearchResult` struct to contain the image search results, and JSON header information.
51
+
Define a `SearchResult` struct to contain the image search results, and JSON header information.
52
52
53
-
```csharp
54
-
struct SearchResult
55
-
{
56
-
public String jsonResult;
57
-
public Dictionary<String, String> relevantHeaders;
58
-
}
59
-
```
53
+
```csharp
54
+
struct SearchResult
55
+
{
56
+
public String jsonResult;
57
+
public Dictionary<String, String> relevantHeaders;
58
+
}
59
+
```
60
60
61
61
## Create and handle a video search request
62
62
63
-
Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created earlier. In the method, perform the following steps:
63
+
1. Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created previously.
64
64
65
-
1. Construct the URI for the search request. Note that the search term toSearch must be formatted before being appended to the string.
65
+
Add code to this method in the steps that follow.
66
66
67
-
```csharp
68
-
67
+
1. Construct the URI for the search request. Format the search term `toSearch` before you append it to the string.
var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(toSearch);
72
73
//...
73
74
```
74
75
75
-
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then use a `StreamReader` to get the JSON string.
76
+
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then, use a `StreamReader` to get the JSON string.
76
77
77
78
```csharp
78
79
//...
@@ -85,7 +86,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
85
86
86
87
## Process the result
87
88
88
-
1. Create the search result object, and extract the Bing HTTP headers. Then return the `searchResult` object.
89
+
1. Create the search result object, and extract the Bing HTTP headers. Then, return the `searchResult` object.
89
90
90
91
```csharp
91
92
var searchResult = new SearchResult();
@@ -101,7 +102,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
Copy file name to clipboardExpand all lines: articles/cognitive-services/Bing-Video-Search/quickstarts/java.md
+52-50Lines changed: 52 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,16 +9,18 @@ manager: nitinme
9
9
ms.service: cognitive-services
10
10
ms.subservice: bing-video-search
11
11
ms.topic: quickstart
12
-
ms.date: 12/09/2019
12
+
ms.date: 05/22/2020
13
13
ms.author: aahi
14
14
---
15
15
# Quickstart: Search for videos using the Bing Video Search REST API and Java
16
16
17
-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple Java application sends an HTTP video search query to the API, and displays the response. While this application is written in Java, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
17
+
Use this quickstart to make your first call to the Bing Video Search API. This simple Java application sends an HTTP video search query to the API and displays the JSON response. Although this application is written in Java, the API is a RESTful Web service compatible with most programming languages.
18
+
19
+
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
18
20
19
21
## Prerequisites
20
22
21
-
* The [Java Development Kit(JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
23
+
* The [Java Development Kit(JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
22
24
23
25
* The [Gson library](https://github.com/google/gson)
24
26
@@ -27,7 +29,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
27
29
28
30
## Create and initialize a project
29
31
30
-
1. Create a new Java project in your favorite IDE or editor, and import the following libraries.
32
+
1. Create a new Java project in your favorite IDE or editor, and import the following libraries:
31
33
32
34
```java
33
35
import java.net.*;
@@ -54,7 +56,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
54
56
}
55
57
```
56
58
57
-
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and a search term. It will return a `SearchResults` object. `host` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
59
+
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and search term. This method returns a `SearchResults` object. For the `host` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
58
60
59
61
```java
60
62
public static SearchResults SearchVideos (String searchQuery) throws Exception {
@@ -67,66 +69,66 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
67
69
68
70
## Construct and send the search request
69
71
70
-
1. In `SearchVideos()`, perform the following steps:
72
+
In the `SearchVideos()` method, perform the following steps:
71
73
72
-
1. construct the URL for your request by combining your API host, path, and encoding your search query. Then use `openConnection()` to create a connection, and add your subscription key to the `Ocp-Apim-Subscription-Key` header.
74
+
1. Construct the URL for your request by combining your API host, path, and encoded search query. Use `openConnection()` to create a connection, and then add your subscription key to the `Ocp-Apim-Subscription-Key` header.
2. Get the response from the API and store the JSON string.
82
+
2. Get the response from the API and store the JSON string.
81
83
82
-
```java
83
-
InputStream stream = connection.getInputStream();
84
-
String response = new Scanner(stream).useDelimiter("\\A").next();
85
-
```
84
+
```java
85
+
InputStream stream = connection.getInputStream();
86
+
String response = new Scanner(stream).useDelimiter("\\A").next();
87
+
```
86
88
87
-
3. Use `getHeaderFields();` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then close the stream and return the result.
89
+
3. Use `getHeaderFields()` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then, close the stream and return the result.
1. Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to take in a JSON string and convert it into an object. Then use `GsonBuilder()` and `toJson()` to create the formatted string.
106
+
Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to convert a JSON string to an object. Then, use `GsonBuilder()` and `toJson()` to create the formatted string.
105
107
106
-
```java
107
-
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
Gson gson = new GsonBuilder().setPrettyPrinting().create();
114
+
return gson.toJson(json);
115
+
}
116
+
```
115
117
116
118
## Send the request and print the response
117
119
118
-
1. In the main method of your application, call `SearchVideos` with your search term. you can then print the HTTP headers stored in the response, as well as the JSON string returned by the API.
120
+
In the main method of your application, call `SearchVideos` with your search term. Then, print the HTTP headers stored in the response and the JSON string returned by the API.
119
121
120
-
```java
121
-
public static void main (String[] args) {
122
+
```java
123
+
public static void main (String[] args) {
122
124
123
-
SearchResults result = SearchVideos(searchTerm);
124
-
//print the Relevant HTTP Headers
125
-
for (String header : result.relevantHeaders.keySet())
0 commit comments