<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 獲取URL請求狀態 本篇簡單介紹如果使用原生API發送HTTP請求 題目: 請求鏈接 https://www.baidu.com/ ,查看返回URL的狀態 把這一部分單獨拿出來,是我覺得有必要更深一步的理解http狀態碼的作用 ### 原生API請求獲取HTTP返回狀態 ``` package com.llg.book.http; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class HttpResponseTest { public static void main(String[] args) throws MalformedURLException { String link = "https://www.baidu.com/"; URL url = new URL(link); System.out.println(getHttpResponseCode(url)); } public static int getHttpResponseCode(URL url) { HttpURLConnection httpurlconnection = null; int responsecode = -1; try { URLConnection urlconnection = url.openConnection(); urlconnection.connect(); if (!(urlconnection instanceof HttpURLConnection)) { // urlconnection.disconnect(); return responsecode; } httpurlconnection = (HttpURLConnection) urlconnection; // httpurlconnection.setFollowRedirects(true); // 獲取返回碼,通過responsecode 就可以知道網頁的狀態,我們也是通過此字段用于判斷請求的資源是否存在 responsecode= httpurlconnection.getResponseCode(); switch (responsecode) { // here valid codes! case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: break; default: httpurlconnection.disconnect(); } } catch (Exception ioexception) { if (httpurlconnection != null) { httpurlconnection.disconnect(); } return responsecode; } return responsecode; } } ``` 執行以上代碼,返回結果 ```200 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看