Use this quickstart to learn how to request search results from your Bing Custom Search instance. Although this application is written in Java, the Bing Custom Search API is a RESTful web service compatible with most programming languages. The source code for this sample is available on GitHub.
Prerequisites
A Bing Custom Search instance. For more information, see Quickstart: Create your first Bing Custom Search instance.
The latest Java Development Kit.
The Gson library.
Create and initialize the application
Create a new Java project in your favorite IDE or editor, and import the following libraries:
import java.io.InputStream; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import javax.net.ssl.HttpsURLConnection; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser;Create a class named
CustomSrchJava, and then create variables for your subscription key, custom search endpoint, and search instance's custom configuration ID.public class CustomSrchJava { static String host = "https://api.bing.microsoft.com"; static String path = "/v7.0/custom/search"; static String subscriptionKey = "YOUR-SUBSCRIPTION-KEY"; static String customConfigId = "YOUR-CUSTOM-CONFIG-ID"; static String searchTerm = "Microsoft"; ...Create another class named
SearchResultsto contain the response from your Bing Custom Search instance.class SearchResults { HashMap<String, String> relevantHeaders; String jsonResponse; SearchResults(HashMap<String, String> headers, String json) { relevantHeaders = headers; jsonResponse = json; } }Create a function named
prettify()to format the JSON response from the Bing Custom Search API.// pretty-printer for JSON; uses GSON parser to parse and re-serialize public static String prettify(String json_text) { JsonParser parser = new JsonParser(); JsonObject json = parser.parse(json_text).getAsJsonObject(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); return gson.toJson(json); }
Send and receive a search request
Create a function named
SearchWeb()that sends a request and returns aSearchResultsobject. Create the request url by combining your custom configuration ID, query, and endpoint information. Add your subscription key to theOcp-Apim-Subscription-Keyheader.public class CustomSrchJava { ... public static SearchResults SearchWeb (String searchQuery) throws Exception { // construct the URL for your search request (endpoint + query string) URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchTerm, "UTF-8") + "&CustomConfig=" + customConfigId); HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); ...Create a stream and store the JSON response in a
SearchResultsobject.public class CustomSrchJava { ... public static SearchResults SearchWeb (String searchQuery) throws Exception { ... // receive the JSON body InputStream stream = connection.getInputStream(); String response = new Scanner(stream).useDelimiter("\\A").next(); // construct result object for return SearchResults results = new SearchResults(new HashMap<String, String>(), response); stream.close(); return results; }Print the JSON response.
System.out.println("\nJSON Response:\n"); System.out.println(prettify(result.jsonResponse));Run the program.