使用java搜索bing azure marketpalce的api

使用java搜索bing azure marketpalce的api,java,api,azure,bing,Java,Api,Azure,Bing,尝试将bing azure marketpalce的搜索api与java结合使用 我有以下代码: import org.apache.commons.codec.binary.Base64; import org.apache.http.client.ResponseHandler; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.

尝试将bing azure marketpalce的搜索api与java结合使用 我有以下代码:

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class BingAPI2 {
    public static void main(String[] args) throws Exception{
        BingAPI2 b = null;
        b.getBing();

    }

public static void getBing() throws Exception {

        HttpClient httpclient = new DefaultHttpClient();

        try {
            String accountKey = "myAccountKey=";
            byte[] accountKeyBytes = Base64.encodeBase64((":" + accountKey).getBytes());
            String accountKeyEnc = new String(accountKeyBytes);

            HttpGet httpget = new HttpGet("https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?$Query=%27Datamarket%27&$format=json");
            httpget.setHeader("Authorization", "Basic <"+accountKeyEnc+">");

            System.out.println("executing request " + httpget.getURI());

            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
            System.out.println("----------------------------------------");

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}
import org.apache.commons.codec.binary.Base64;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.BasicResponseHandler;
导入org.apache.http.impl.client.DefaultHttpClient;
公共类BingAPI2{
公共静态void main(字符串[]args)引发异常{
bingapi2b=null;
b、 getBing();
}
public static void getBing()引发异常{
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
字符串accountKey=“myAccountKey=”;
byte[]accountKeyBytes=Base64.encodeBase64((“:”+accountKey.getBytes());
String accountKeyEnc=新字符串(accountKeyBytes);
HttpGet HttpGet=新的HttpGet(“https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?$Query=%27Datamarket%27&$format=json”);
setHeader(“授权”、“基本”);
System.out.println(“正在执行请求”+httpget.getURI());
//创建一个响应处理程序
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httpget,responseHandler);
System.out.println(“--------------------------------------------------------”;
系统输出打印LN(响应库);
System.out.println(“--------------------------------------------------------”;
}最后{
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpclient.getConnectionManager().shutdown();
}
}
}
我得到一个错误:

线程“main”中出现异常 org.apache.http.client.HttpResponseException:授权类型 不支持您提供的。仅支持Basic和OAuth


我首先看到的是你的台词

byte[]accountKeyBytes=Base64.encodeBase64((“:”+accountKey.getBytes())

应改为:

byte[]accountKeyBytes=Base64.encodeBase64((accountKey+“:“+accountKey).getBytes())

您使用apache库进行此操作还有什么原因吗?我用于从bing获取json对象的代码使用java.net,如下所示:

import java.net.URLConnection;
import java.net.URL;
import java.io.InputStreamReader;

class BingJson{

  JSONObject getJSONfromBing(String term){
  try{
    URLConnection c = new URL(term).openConnection();
    String key = (DatatypeConverter.printBase64Binary(("XXX" + ":" + "XXX").getBytes("UTF-8")));
    c.setRequestProperty("Authorization", String.format("Basic %s",key));
    c.connect();
    //etc.
  }
}
要构建json对象,请遵循以下代码: