【问题标题】:Can't read data from url due to cloudflare由于 cloudflare,无法从 url 读取数据
【发布时间】:2016-12-31 20:31:25
【问题描述】:

每当我编译时,我都会得到这个:

线程“main”java.io.IOException 中的异常:服务器返回 HTTP 响应代码:403 用于 URL:链接位于 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(未知 来源)在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知 源)在 java.net.URL.openStream(未知源)在 readdata.aaa.main(aaa.java:15)

我的脚本是:

package readdata;

import java.net.*;
import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class aaa 
{
    public static void main(String[] args) throws Exception {

        URL oracle = new URL(" the link ");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
            a.append(inputLine);
        in.close();


        int i = 0;
        Pattern p = Pattern.compile("Open");
        Matcher m = p.matcher( a );
        while (m.find()) {
            i++;
            System.out.println(i);
        }
    }

}

我是否可以绕过 cloudflare 从 URL 读取数据?

【问题讨论】:

    标签: url cloudflare


    【解决方案1】:

    之前

    URL oracle = new URL(" the link ");
    

    插入:

    System.setProperty("http.agent", "Chrome");
    

    这可能是因为 CloudFlare 阻止了未知代理请求,因此此代码将 User-Agent 设置为 CloudFlare 识别的 Chrome。

    【讨论】:

    • 不敢相信我把头发扯掉了
    猜你喜欢
    • 2019-03-14
    • 2012-08-06
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2021-08-19
    • 2020-11-20
    相关资源
    最近更新 更多