1

我正在尝试 Azure Bing 搜索 API(仅适用于 url 链接),想知道是否有人可以帮助改进我的 java,以便不需要 1.5 到 2.5 秒即可获得 API 返回。

我想我已经找到了瓶颈,但不知道如何解决它。

具体来说,在提供了我的搜索词和 accountKey 之后,我设置了一个 URLConnection。到目前为止,应用程序运行——而且速度很快——但是我没有得到任何回报。为此,我创建了一个 InputStream 并运行一个 while 循环以将所有字符添加到 StringBuilder。这是显着降低速度的部分。

有没有比我正在做的更快的方法来获取搜索结果?我没有尝试使用 json 或 Bing 的选项来获取 tarball 的结果,但不确定这些是否会显着改变这一点。

详细信息——操作系统(我在基本的 CentOS 机器和 Windows 7 机器上进行了尝试——两者都使用 eclipse 中的控制台来查看结果)。Myspeedtest.net 显示 12Mbps 下降和 1Mbps 上升。

public static void api(String st) throws IOException{
        String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?Query=%27"+st+"%27";

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

        URL url = new URL(bingUrl);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);     
        //FROM HERE DOWN IS WHAT SEEMS TO TAKE FOREVER. 
        InputStream is = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);

        int numCharsRead;
        char[] charArray = new char[1024];
        StringBuilder sb = new StringBuilder();
        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }

        System.out.println(sb.toString());
    }
编辑 #### 编辑 ### 编辑 ##### 编辑

我刚刚尝试了另一种使用 json 和 Apache httpclient 的方法。同样,这也很好用,但速度很慢。也许是 Msft 的服务?

package com.search;

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 getBing() throws Exception {

        HttpClient httpclient = new DefaultHttpClient();

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

            HttpGet httpget = new HttpGet("https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=%27Datamarket%27&$top=10&$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();
        }
    }

}
4

0 回答 0