【问题标题】:java search google image - error 302 - The document has movedjava搜索谷歌图片 - 错误302 - 文档已移动
【发布时间】:2017-05-03 20:27:05
【问题描述】:

我在尝试从 Java 进行谷歌搜索时遇到问题。

 URL url = new URL("http://www.google.com/cse?searchtype=image&as_filetype=jpg&start=0&num=5&q=booba&client=google-csbe&cx=xxxxxxxxxxxxxxxxxxx");
        URLConnection connection = url.openConnection();

        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((line = reader.readLine()) != null) {
            builder.append(line);
            System.out.println(line);
        }

结果是这样的:

302 Moved
The document has moved

有人可以帮帮我吗?

谢谢

【问题讨论】:

  • 其余的响应头是什么样的?
  • 嗨,谢谢,这是邮件的其余部分: 302 已移动

    302 已移动

    文档已移动cse.google.com/…>。

标签: java google-search


【解决方案1】:

这就是 Google 选择将您重定向到其他网址的方式。 “原始”HTTP 响应将如下所示:

HTTP/1.1 302 Found
Date: Sat, 01 May 2017 20:51:40 GMT
Content-Type: text/html; charset=utf-8
Location: https://cse.google.com/cse?searchtype=image&as_filetype=jpg&start=0&num=5&q=booba&client=google-csbe&cx=xxxxxxx-xxxxxxx
Connection: Keep-Alive
Content-Length: 325

<!DOCTYPE html>
<html>
...
</html>

这里的关键是 Location 标头,它指示新的 URL。 Java 的URLConnection 应该自动跟随重定向。但是,当重定向会升级连接 from HTTP to HTTPS(就像这里一样)时,情况并非如此。

您应该使用 HTTPS 安全地启动连接,或者从每个 302 响应中解析出 Location 标头并创建到该 URL 的新连接。

【讨论】:

  • 我尝试使用 https 发起连接。现在我有一个 IOException java.io.IOException: Server returned HTTP response code: 400 for URL: cse.google.com
  • 您能否发布整个原始响应的要点或粘贴箱(就像我在答案中引用的那样)?
  • 谢谢,我没有更多的回应,现在我只有一个 IO 异常:sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) 此行抛出异常: connection.getInputStream(),
  • 如果我直接在谷歌上做,我有这个结果: Error 400 (Bad Request)!!1 看来我的请求不正确
  • 好的,等你的请求正确后如果还是不行就回来
【解决方案2】:

我更改了我的请求,现在,我遇到了 403 错误:

{ "error": { "errors": [ {
 "domain": "usageLimits", 
 "reason": "dailyLimitExceededUnreg", 
 "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use  requires signup.",
 "extendedHelp": "code.google.com/apis/console"; } ], 
 "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded

这可能是由谷歌服务器密钥引起的问题,我应该在谷歌开发者的控制台中获取,但是无法访问控制台,当我尝试去那里时它给了我一个空白页面:

https://console.developers.google.com/project

你知道为什么吗?

【讨论】:

  • 您提供的链接对我来说很好,尽管是在移动设备上。尝试通常的嫌疑人:清除缓存和 cookie,重新启动浏览器等
  • 谢谢,问题出在 chrome navigator,我在 XP 上,不再更新了
【解决方案3】:

好的, 我找到了答案,要使用自定义搜索,你需要有一个 cx 值和一个键值,你可以得到 https://console.developers.google.com/ 为了使您的请求正常工作,您还需要将 start 参数设置为至少 1,因为 0 会给您一个错误 400 无效值

这篇文章对我帮助很大:

Google image search says api no longer available

这是我的要求:

https://www.googleapis.com/customsearch/v1?key=xxxxxxx&searchtype=image&start=1&num=5&q=monkey&as_filetype=png&client=google-csbe&output=xml_no_dtd&cx=xxxxxx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多