微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使网站已在浏览器中正确加载,仍会获取403响应代码

如何解决即使网站已在浏览器中正确加载,仍会获取403响应代码

我正在尝试使用httpurlconnection获取响应代码,但是即使该网站在浏览器上正常访问,也会获得“ 403”作为响应代码

URL:“ https://www.texanscu.org/home/home”

这是我正在使用的代码

try {
     String url = "https://www.texanscu.org/home/home";

     HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

     conn.setRequestMethod("GET");
     conn.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/83.0.4103.116 Safari/537.36");
     conn.setConnectTimeout(2000);
     conn.setInstanceFollowRedirects(false);
     conn.setReadTimeout(100000);
     conn.connect();

     int responseCode = conn.getResponseCode();

    } catch(Exception e) {
         logger.error("Caught exception : {}",e.getMessage);
    }

也不例外。只是我得到的响应代码为“ 403”。

解决方法

我对您的代码做了一些修改,以查看服务器的实际响应。

 try {
            String url = "https://www.texanscu.org/";

            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/84.0.4147.135 Safari/537.36");
            conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            conn.setConnectTimeout(100000);
            conn.setInstanceFollowRedirects(false);
            conn.setReadTimeout(100000);
            conn.connect();

            int responseCode = conn.getResponseCode();
            System.out.println(responseCode);
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
            String strCurrentLine;
            while ((strCurrentLine = br.readLine()) != null) {
                System.out.println(strCurrentLine);
            }


        } catch(Exception e) {
            e.printStackTrace();
        }

因此,当我执行此代码时,我看到以下输出:

<head>
<title>Attention Required! | Cloudflare</title>
<meta name="captcha-bypass" id="captcha-bypass" />
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex,nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
<style type="text/css">body{margin:0;padding:0}</style>


<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script><!--<![endif]-->
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script><!--<![endif]-->

</head>
<body>
  <div id="cf-wrapper">
    <div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
    <div id="cf-error-details" class="cf-error-details-wrapper">
      <div class="cf-wrapper cf-header cf-error-overview">
        <h1 data-translate="challenge_headline">One more step</h1>
        <h2 class="cf-subheadline"><span data-translate="complete_sec_check">Please complete the security check to access</span> www.texanscu.org</h2>
      </div><!-- /.header -->
      
      <div class="cf-section cf-highlight cf-captcha-container">
        <div class="cf-wrapper">
          <div class="cf-columns two">
            <div class="cf-column">
            
              <div class="cf-highlight-inverse cf-form-stacked">
                <form class="challenge-form" id="challenge-form" action="/?__cf_chl_captcha_tk__=8f811f0d4e8be53ef88568630d8c627b6a8639a6-1598364414-0-AXcy5nmycYBIOZVpr4NiQNNpsvz-TeYA4kD6NYOYQq8A9OjdxedaBdcfaEp4DM-P6EnhMFryAMIv8_Vi3PM3ukkKw8W4aFv0W4FXhYk4eJgcmPWlA6XdiAQBfIRWqmV7ORtKClPdGo9CgujUYWdpkGr_3hGiUU_bLFp9jf8mF-nCM3s9nex_0MiA916wQSCs-nhaM8_jFGdJ2VmJjczihFz8MFed_zVHNzLG4HHQdcrOl13P4jZy9Y_nhJfAyhVG0ngQXE8y-Slb_c5gvcfGGDa8vrxTpLgrQEF2-SwqkjhJTApfSUn6Y3mtjZ9ZYrA28NDZC1ngeit3IOga5pxB2wcZuYHfatTHy832J_itWa8MrtbDQV_DSWwGroAdC9q10MVYI0CIPzcxzvOrWSUYjlPYRxEKE_cw1mvO5hxsQuPtSlHIMs0bIHUpZl88F16Ki1xr8FEgqGM8aU2-VFlzYjKHh89qHe1MoapqHmZ31Na5Q0LAbGJdl69lGFGhUczHqWL9D015U4Jfpmim3203E23qb5vLnzBu8kJf6ygKDvKn" method="POST" enctype="application/x-www-form-urlencoded">
 
  <input type="hidden" name="cf_captcha_kind" value="h">
  <input type="hidden" name="vc" value="">
  
  <script type="text/javascript" src="/cdn-cgi/scripts/hcaptcha.challenge.js" data-type="normal"  data-ray="5c85e0576bec0faa" async data-sitekey="33f96e6a-38cd-421b-bb68-7806e1764460"></script>
  
  <noscript id="cf-captcha-bookmark" class="cf-captcha-info">
  <h1 data-translate="turn_on_js" style="color:#bd2426;">Please turn JavaScript on and reload the page.</h1>
  </noscript>
  <div id="no-cookie-warning" data-translate="turn_on_cookies" style="display:none">
    <h1 data-translate="turn_on_cookies" style="color:#bd2426;">Please enable Cookies.</h1>
  </div>
  <script type="text/javascript">
  //<![CDATA[
    var a = function() {try{return !!window.addEventListener} catch(e) {return !1} },b = function(b,c) {a() ? document.addEventListener("DOMContentLoaded",b,c) : document.attachEvent("onreadystatechange",b)};
      b(function(){
        var cookiesEnabled=(navigator.cookieEnabled)? true : false;
        if(!cookiesEnabled){
          var q = document.getElementById('no-cookie-warning');q.style.display = 'block';
        }
      });
  //]]>
  </script>
  <div id="trk_captcha_js" style="background-image:url('/cdn-cgi/images/trace/captcha/nojs/h/transparent.gif?ray=5c85e0576bec0faa')"></div>
</form>
              </div>
            </div>
            <div class="cf-column">
              <div class="cf-screenshot-container">
                <span class="cf-no-screenshot"></span>
              </div>
            </div>
          </div><!-- /.columns -->
        </div>
      </div><!-- /.captcha-container -->

      <div class="cf-section cf-wrapper">
        <div class="cf-columns two">
          <div class="cf-column">
            <h2 data-translate="why_captcha_headline">Why do I have to complete a CAPTCHA?</h2>
            
            <p data-translate="why_captcha_detail">Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.</p>
          </div>

          <div class="cf-column">
            <h2 data-translate="resolve_captcha_headline">What can I do to prevent this in the future?</h2>
            

            <p data-translate="resolve_captcha_antivirus">If you are on a personal connection,like at home,you can run an anti-virus scan on your device to make sure it is not infected with malware.</p>

            <p data-translate="resolve_captcha_network">If you are at an office or shared network,you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.</p>
           
              <p data-translate="resolve_captcha_privacy_pass"> Another way to prevent getting this page in the future is to use Privacy Pass. You may need to download version 2.0 now from the <a href="https://chrome.google.com/webstore/detail/privacy-pass/ajhmfdgkijocedmfjonnpjfojldioehi">Chrome Web Store</a>.</p>
              
            
          </div>
        </div>
      </div><!-- /.section -->
      

      <div class="cf-error-footer cf-wrapper">
  <p>
    <span class="cf-footer-item">Cloudflare Ray ID: <strong>5c85e0576bec0faa</strong></span>
    <span class="cf-footer-separator">&bull;</span>
    <span class="cf-footer-item"><span>Your IP</span>: 178.221.185.37</span>
    <span class="cf-footer-separator">&bull;</span>
    <span class="cf-footer-item"><span>Performance &amp; security by</span> <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">Cloudflare</a></span>
    
  </p>
</div><!-- /.error-footer -->


    </div><!-- /#cf-error-details -->
  </div><!-- /#cf-wrapper -->

  <script type="text/javascript">
  window._cf_translation = {};
 
</script>
</body>
</html>

Cloudflare正在保护您尝试访问的网站,因此他们在服务器上使用反DoS系统。“ DoS”表示拒绝服务攻击。例如,某人配置了数千台计算机来访问网站,以使其超载。这些自动攻击是由机器人或简单的“机器人”完成的。 显然,系统认为您是机器人。因此,您无法到达该终结点,而这些代码都是Java代码。

编辑:如果找到此库: https://github.com/iambluedev1/cfscrape-java 它用于绕过Cloudflare的反机器人页面。你可以试试看。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。