抓取分公司對外浮動IP

透過ASP.NET抓取分公司對外浮動IP

抓取分公司對外浮動IP

有鑑於常常需要抓到個分公司的對外浮動IP,故完成該功能。

程式發想:透過各分公司內伺服器上放一支服務程式,固定某時間將連上總公司的網頁程式後,透過

HTTP_X_FORWARDED_FOR取得分公司IP,並回總公司資料庫內TABLE。

 
網頁程式 SetUpSQLServerIP.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using GonGinLibrary;

namespace WebApplication1
{
    public partial class SetUpSQLServerIP : System.Web.UI.Page
    {
        static String 連接字串 = " Data Source=XXX.XXX.XXX.XXX,1433;Initial Catalog=DB;Integrated Security=False; " +
                          " User ID=sa;Password=;MultipleActiveResultSets=True;Connect Timeout=60";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request["Location"] == null)
            {
                //Response.Write("alert('" + "INVALID" + "')");
                Label1.Text = "無效的參數";
            }
            else
            {
                //GonGinVariable.SqlConnectString
                //using (SqlConnection sqlcn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MISConnectionString"].ToString()))
                using (SqlConnection sqlcn = new SqlConnection(連接字串))
                {
                    //using (SqlCommand sqlcmd = new SqlCommand("INSERT CLIENT_IP (SERVER,IP,CRDATE) VALUES (@SERVER,@IP,GETDATE())", sqlcn))
                    using (SqlCommand sqlcmd = new SqlCommand("UPDATE CLIENT_IP SET IP=@IP,CRDATE=GETDATE() WHERE SERVER=@SERVER AND IP<>@IP", sqlcn))
                    {
                        try
                        {
                            sqlcn.Open();
                            sqlcmd.Parameters.AddWithValue("@SERVER", this.Request["Location"]);
                            sqlcmd.Parameters.AddWithValue("@IP", GetClientIP());
                            sqlcmd.ExecuteNonQuery();
                            Label1.Text = "寫入成功";
                        }
                        catch (Exception ex)
                        {
                            Label1.Text = "寫入失敗:" + ex.Message;
                        }
                    }

                }
            }
        }
        public static string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || (result != null && String.IsNullOrEmpty(result)))
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || (result != null && String.IsNullOrEmpty(result)))
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result.Split(new Char[] { ',' })[0];

        }
    }
}/pre>
	
伺服端服務:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            string Previous_IP = "Previous";
            string Current_IP = string.Empty;
            int iMinsToSleep = 3;
            string strUrl;
            WebRequest wr;
            HttpWebResponse response;
            Uri uri;
            System.Diagnostics.EventLog eventLog = new EventLog();
            while (true)
            {
                try
                {
                    strUrl = "http://www.XXXX.com.tw/SetUpSQLServerIP.aspx?Location=" + _DBName;
                    uri = new Uri(strUrl);
                    wr = WebRequest.Create(uri);
                    response = (HttpWebResponse)wr.GetResponse();
                    response.Close();
                }
                catch (Exception ex)
                {
                    eventLog.Source = "DB";
                    eventLog.EnableRaisingEvents = true;
                    eventLog.WriteEntry("取得IP發生錯誤:\r\n" + ex.Message, EventLogEntryType.Error);
                }

                for (int i = 0; i < iMinsToSleep * 60; i++)
                {
                    if (backgroundWorker2.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }

 

 

 

興滿逸筑2013年全新完工,位於恆春鎮外圍離市區只要2分鐘車程,有豪華的外觀、有美麗的庭院、也有溫馨舒適的房間,這裡車流量不多,空氣不錯,適合家族或成群結黨的朋友們一同旅遊的好地方。

http://sinmaniz.tw/index.html