Java https请求获取curseforge网站的403响应代码

时间:2019-09-30 18:24:59

标签: java https

我尝试从https://www.curseforge.com/projects/291874重定向到https://www.curseforge.com/minecraft/mc-mods/serene-seasons,但是所有尝试都响应403。我尝试使用HttpURLConnection,HttpsURLConnection和URLConnection,但是没有任何作用。 如何在这个网站上提出要求? 浏览器执行以下两个步骤: 连接www.curseforge.com:443 HTTP / 1.1 GET / projects / 291874 HTTP / 1.1 如何在Java上执行此操作? 请在回答之前检查您的代码(有问题的第一个链接)

String url  = "https://www.curseforge.com/projects/291874";
        try {
        HttpURLConnection con1 = (HttpURLConnection)(new URL(url).openConnection());
        con1.setRequestProperty("Host", "www.curseforge.com");
        con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0");
        con1.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        con1.setRequestProperty("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
        con1.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        con1.setRequestProperty("DNT", "1");
        con1.setRequestProperty("Connection", "keep-alive");
        con1.setRequestProperty("Upgrade-Insecure-Requests", "1");
        con1.connect();
        //con1.getInputStream();
        System.out.println(con1.getResponseCode());
        System.out.println(con1.getHeaderField("Location"));
        } catch (IOException e) {
            e.printStackTrace();
        }

0 个答案:

没有答案