以下のサンプルでは、レスポンスを標準出力に出力している。
package jp.ac.dendai.im.web;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;
public class BingSearcher {
// Query URL for Bing Search API
public static final String rootURL = "https://api.datamarket.azure.com/Bing/Search/Web";
// Query URL for Bing Search API - Web Results Only
//public static final String rootURL = "https://api.datamarket.azure.com/Bing/SearchWeb/Web";
public static final String accountKey = "your account key=";
private String query;
private Document document;
// main
public static void main(String[] args) {
BingSearcher searcher = new BingSearcher();
searcher.setQuery("東京電機大学");
searcher.run();
searcher.show();
}
public void setQuery(String query) {
this.query = query;
}
public void run() {
// Basic 認証のための設定
Authenticator.setDefault(new BingAuthenticator(accountKey));
try {
URL url = new URL(rootURL + "?Query=%27" + URLEncoder.encode(query, "utf-8") + "%27");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
// DOMツリーの構築
document = buildDocument(inputStream, "utf-8");
}
catch (IOException e) {
e.printStackTrace();
}
}
/** DOM Tree の構築 */
public Document buildDocument(InputStream inputStream, String encoding) {
Document document = null;
try {
// DOM実装(implementation)の用意 (Load and Save用)
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS implementation = (DOMImplementationLS)registry.getDOMImplementation("XML 1.0");
// 読み込み対象の用意
LSInput input = implementation.createLSInput();
input.setByteStream(inputStream);
input.setEncoding(encoding);
// 構文解析器(parser)の用意
LSParser parser = implementation.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// DOMの構築
document = parser.parse(input);
}
catch (Exception e) {
e.printStackTrace();
}
return document;
}
/** DOM Tree の内容を表示 */
public void show() {
try {
showTree(document.getDocumentElement(), 0); // root 要素を与える
}
catch (DOMException e) {
System.err.println("DOMエラー:" + e);
}
}
/** 引数 node 以下の tree を表示 */
private void showTree(Node node, int depth) {
String indent = "";
for(int i = 0; i < depth; i++) {
indent += " ";
}
for(Node current = node.getFirstChild();
current != null;
current = current.getNextSibling()) {
if(current.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(indent + current.getNodeName() + " {");
showTree(current, depth + 1); // さらに子ノードを見る (再帰)
System.out.println(indent + "}");
}
else if(current.getNodeType() == Node.TEXT_NODE
&& current.getNodeValue().trim().length() != 0)
System.out.println(indent + current.getNodeValue());
else if(current.getNodeType() == Node.CDATA_SECTION_NODE)
System.out.println(indent + current.getNodeValue());
; // 上記以外のノードでは何もしない
}
}
}
// Basic 認証用のクラス
class BingAuthenticator extends Authenticator {
private String accountKey;
public BingAuthenticator(String accountKey) {
super();
this.accountKey = accountKey;
}
protected PasswordAuthentication getPasswordAuthentication() {
final String username = accountKey;
final String password = accountKey;
return new PasswordAuthentication(username, password.toCharArray());
}
}
Microsoft は Java 用のライブラリやサンプルプログラムを提供していないが、 第三者が開発しているものを利用することもできる。