相关疑难解决方法(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万
查看次数

java.io.IOException:服务器返回HTTP响应代码:403表示URL

我想从url下载mp3文件:"http://upload13.music.qzone.soso.com/30671794.mp3",我总是得到java.io.IOException:服务器返回HTTP响应代码:403为URL.但是使用浏览器打开网址时没关系.以下是我的代码的一部分:

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
    URL url = new URL(link);

    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

    String contentType = urlConn.getContentType();

    System.out.println("contentType:" + contentType);

    InputStream is = urlConn.getInputStream();
    bis = new BufferedInputStream(is, 4 * 1024);
    bos = new BufferedOutputStream(new FileOutputStream(
    fileName.toString()));?
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?提前致谢!

java http http-status-code-403

20
推荐指数
3
解决办法
8万
查看次数

处理响应代码:403,用于具有clojure enlive的URL

我正在尝试使用enlive的html-resource函数来抓取网页的内容,但我得到了响应403,因为我不是来自浏览器.我想这可以在Java中覆盖(在这里找到答案),但我会喜欢看一种处理这个问题的clojure方法.也许这可以通过为html-resource函数提供参数来实现,但我没有遇到过如何以及需要作为参数传递的示例.任何建议将不胜感激.

谢谢.

java http clojure http-status-code-403 enlive

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

标签 统计

http-status-code-403 ×3

java ×3

http ×2

clojure ×1

enlive ×1