相关疑难解决方法(0)

403禁止使用Java但不禁用Web浏览器?

我正在编写一个小型Java程序来获取给定Google搜索词的结果数量.出于某种原因,在Java中我得到403 Forbidden但我在Web浏览器中获得了正确的结果.码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;


public class DataGetter {

    public static void main(String[] args) throws IOException {
        getResultAmount("test");
    }

    private static int getResultAmount(String query) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(new URL("https://www.google.com/search?q=" + query).openConnection()
                .getInputStream()));
        String line;
        String src = "";
        while ((line = r.readLine()) != null) {
            src += line;
        }
        System.out.println(src);
        return 1;
    }

}
Run Code Online (Sandbox Code Playgroud)

而错误:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.google.com/search?q=test
    at …
Run Code Online (Sandbox Code Playgroud)

java http-status-code-403

43
推荐指数
2
解决办法
7万
查看次数

服务器为 URL 返回 HTTP 响应代码 503

我可以访问网站kissmanga.com 但我无法通过程序访问它。我修复了之前遇到的错误 403,但现在出现错误 503。

    URL url = new URL("http://kissmanga.com/");
    System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36"); 
    BufferedReader bf = new BufferedReader(new InputStreamReader(url.openStream()));

    String str;
    while((str = bf.readLine()) != null){
        System.out.println(str);
    }


 Error that I get:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 503 for URL: http://kissmanga.com/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at KissManga.main(KissManga.java:10)
Run Code Online (Sandbox Code Playgroud)

好的,这段代码可以解决一个烦人的小问题。我没有得到完整的 html,但只有 2/3。

    HtmlUnitDriver driver = new HtmlUnitDriver();
    driver.get("http://kissmanga.com/");
    Thread.sleep(5000);
    System.out.println(driver.getPageSource());
    driver.quit();
Run Code Online (Sandbox Code Playgroud)

java url

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

http-status-code-403 ×1

url ×1