[ASP.Net MVC] 取得 Request 常用資訊 (IP, 作業系統, 瀏覽器, 呼叫網址)

[ASP.NET MVC] 取得 Request 常用資訊 (IP, 作業系統, 瀏覽器, 呼叫網址)

在建置網頁的時候,有時候會需要知道來源端它的環境是什麼,可能是需要記錄來源的 IP,或是依照來源的作業系統而顯示對應的畫面。
在 ASP.Net MVC 裡面想要知道來源端的資訊,全部都放在 Request 這個物件裡面。
接下來我就展示 Request 內常用的資訊,如果想了解 Request 內的資訊,我在後面也放上完整取得 Rquest 內的資訊。

Request 常用資訊

常用資訊裡面我列出了 5 個項目,分別是來源 IP, 作業系統, 瀏覽器, 呼叫網址等資訊。
看看執行的畫面

程式碼

方法程式碼

列出所有 Request 資訊

如果想要完整了解 Request 內所有的資訊,可以用此語法查看

執行後呈現的畫面

顯示的資訊太多,後面就拍不到了,大家可以自己試試看喔。

相關學習文章

如果你在學習上有不懂的地方,需要諮詢服務,可以參考站長服務,我想辨法解決你的問題
如果文章內容有過時、不適用或錯誤的地方,幫我在下方留言通知我一下,謝謝

加入社團一起討論

關注我的 IG

6 thoughts on “[ASP.NET MVC] 取得 Request 常用資訊 (IP, 作業系統, 瀏覽器, 呼叫網址)

  1. 為什麼我會抓到自己內網的IP 而不是訪客的真實IP呢?

    1. 你是不是連自己的內網呢?是的話就會出現內網IP喔
      如果是訪客身份去連別人的網站,才會出現訪客的 IP

      1. 我是從家裡的手機網路 和 家裡的網路 連網站 抓到的都是內網 IP 讓我覺得納悶

        1. 我這裡提供其他取 ip 的方法給你參考

          ///

          /// 取得本機 IP Address
          ///

          ///
          public static List GetHostIPAddress()
          {
          List
          lstIPAddress = new List();
          IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
          foreach (IPAddress ipa in IpEntry.AddressList)
          {
          if (ipa.AddressFamily == AddressFamily.InterNetwork)
          lstIPAddress.Add(ipa.ToString());
          }
          return lstIPAddress;
          }

          ///

          /// 取得遠端呼叫者ip
          ///

          ///
          public string GetClientIP()
          {
          string ClientIP = "";
          if (Request.ServerVariables["HTTP_VIA"] == null)
          {
          ClientIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
          }
          else
          {
          ClientIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
          }
          ClientIP = ClientIP.Replace("::1", "127.0.0.1");
          return ClientIP;
          }

          ///

          /// 取得外網 IP Address
          ///

          ///
          public static string GetExtranetIPAddress()
          {
          HttpWebRequest request = HttpWebRequest.Create("http://www.whatismyip.com.tw") as HttpWebRequest;
          request.Method = "GET";
          request.ContentType = "application/x-www-form-urlencoded";
          request.UserAgent = "Mozilla/5.0";
          string ip = string.Empty;
          WebResponse response = request.GetResponse();
          using (StreamReader reader = new StreamReader(response.GetResponseStream()))
          {
          string result = reader.ReadToEnd();
          string pattern = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
          ip = Regex.Match(result, pattern).ToString();
          }
          return ip;
          }

          1. 跑 GetExtranetIPAddress() 裡面的 WebResponse response = request.GetResponse(); 會出現 基礎連接已關閉: 連接意外關閉

          2. 太久沒連了,對方已經不支援這樣取了

            我提供另外取得對外ip的方法

            ///

            /// 取得外網 IP Address
            ///

            ///
            public static string GetExtranetIPAddress()
            {
            HttpWebRequest request = HttpWebRequest.Create(“http://ipinfo.io/ip”) as HttpWebRequest;
            request.Method = “GET”;
            request.ContentType = “application/x-www-form-urlencoded”;
            request.UserAgent = “Mozilla/5.0″;
            string ip = string.Empty;
            WebResponse response = request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
            string result = reader.ReadToEnd();
            string pattern = @”\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”;
            ip = Regex.Match(result, pattern).ToString();
            }
            return ip;
            }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

four × 1 =


The reCAPTCHA verification period has expired. Please reload the page.