Google Custom Search API错误

时间:2015-09-17 18:16:38

标签: google-api google-custom-search httpexception

我使用Google Cloud Platform进行免费试用 https://cloud.google.com/free-trial/

在我的java应用程序中使用自定义搜索API

我在我的应用程序中编写以下代码

 URL url = new URL("https://www.googleapis.com/customsearch/v1?key="+key+ 
 "&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");

while ((output = br.readLine()) != null) {

    if(output.contains("\"link\": \"")){                
        String link=output.substring(output.indexOf("\"link\": \"")+("\"link\": \"").length(), output.indexOf("\","));

        System.out.println(link);       //Will print the google search links
    }     
}
conn.disconnect();       

它有时给了我一个结果,之后它给了我以下错误,并且无论查询字符串发生了这个错误

线程“main”中的异常java.io.IOException:服务器返回HTTP响应代码:403为URL:https://www.googleapis.com/customsearch/v1?key=????&cx=013036536707430787589:_pqjad5hr1a&q= ?????     at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

请问哪里有问题?

1 个答案:

答案 0 :(得分:0)

403特别是Google的禁止错误 - 这意味着您发送的请求太快,他们认为您使用机器人或自动化

如果您在浏览器中加载该查询并观察请求 - 请继续快速刷新,直到您看到显示

的html响应为止
We're sorry...

... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

See Google Help for more information.