无法从java访问URL,但可以从浏览器(Chrome)访问

问题描述 投票:0回答:1

我正在开发一个从主页获取数据的应用程序。我在访问特定 URL 时遇到问题:“https://www.intel.com/content/www/us/en/products/sku/218824/intel-w790-chipset/specifications.html”。虽然我可以从浏览器 (Chrome) 访问此 URL,没有任何问题,但尝试从我的 Java 应用程序进行连接。结果是 403 状态代码(禁止)。任何人都可以提供有关如何解决此问题的指导吗?任何帮助将不胜感激。预先感谢!

相关代码:

 String url = "https://www.intel.com/content/www/us/en/products/sku/218824/intel-w790-chipset/specifications.html";

        try {
            boolean redirected = false;
            URL obj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

            // Set request method to GET
            conn.setRequestMethod("GET");

            // Add request headers
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
            conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Connection", "keep-alive");

            // Set the cookies allowed property
            conn.setAllowUserInteraction(true);

            // Handle the response
            int responseCode = conn.getResponseCode();
    ...

java google-chrome http
1个回答
0
投票

正如 Polk 所评论的那样,有问题的行是:

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
通过那条线,我们看到:

响应代码 = 403

消息=禁止

如果我们注释掉该行,则其余行在 Java 22 上运行良好。

String url = "https://www.intel.com/content/www/us/en/products/sku/218824/intel-w790-chipset/specifications.html"; boolean redirected = false; URL obj = null; try { obj = new URL( url ); HttpURLConnection conn = ( HttpURLConnection ) obj.openConnection( ); // Set request method to GET conn.setRequestMethod( "GET" ); // Add request headers //conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); conn.setRequestProperty( "Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ); conn.setRequestProperty( "Accept-Language" , "en-US,en;q=0.5" ); conn.setRequestProperty( "Accept-Encoding" , "gzip, deflate, br" ); conn.setRequestProperty( "Connection" , "keep-alive" ); // Set the cookies allowed property conn.setAllowUserInteraction( true ); // Handle the response final int responseCode = conn.getResponseCode( ); final String message = conn.getResponseMessage( ); System.out.println( "responseCode = " + responseCode ); System.out.println( "message = " + message ); } catch ( MalformedURLException e ) { throw new RuntimeException( e ); } catch ( ProtocolException e ) { throw new RuntimeException( e ); } catch ( IOException e ) { throw new RuntimeException( e ); }

响应代码=200

消息=确定

© www.soinside.com 2019 - 2024. All rights reserved.