Foxtable(狐表)用户栏目专家坐堂 → 1秒内判断内网通还是外网通并且自动设置数据源


  共有2776人关注过本帖平板打印复制链接

主题:1秒内判断内网通还是外网通并且自动设置数据源

帅哥哟,离线,有人找我吗?
supwork
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:688 积分:4903 威望:0 精华:0 注册:2013/10/27 17:14:00
1秒内判断内网通还是外网通并且自动设置数据源  发帖心情 Post By:2015/7/9 10:32:00 [只看该作者]

'解析文件
Dim strs As String = FileSys.ReadAllText("" & ProjectPath & "123.txt")
strs = strs.Replace(vblf,"")
Dim rs() As String = strs.Split(vbcr)
Dim nwzip As String = "" '内网主IP
Dim nwLip As String = "" '内网LOG IP
Dim wwzip As String = "" '外网主IP
Dim wwLip As String = "" '外网LOG IP
If rs.Length >=1 Then
nwzip = DecryptText(rs(0),"Es2%","ty47^.")
End If
If rs.Length >=2 Then
nwLip = DecryptText(rs(1),"Es2%","ty47^.")
End If
If rs.Length >=3 Then
wwzip = DecryptText(rs(2),"Es2%","ty47^.")
End If
If rs.Length >=4 Then
wwLip = DecryptText(rs(3),"Es2%","ty47^.")
End If
'msgbox(nwzip)
'msgbox(nwLip)
'msgbox(wwzip)
'msgbox(wwLip)
msgbox(1)
'内网外网判断
Dim url As String = "http://192.168.1.8:8008/test"  '请按实际升级的路径填写
Dim FTT As String
try
    Dim rqst As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
    rqst.TimeOut = 1000 '一秒
    Dim rsps As System.Net.HttpWebResponse = rqst.GetResponse
    Dim stm As System.IO.Stream = rsps.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm)
    Dim str As String = reader.ReadToEnd
   
    Dim mc = System.Text.RegularExpressions.Regex.Matches(str, "(?<=\<BODY\>+.+;+).+(?=\</BODY?\>+)")
    For i As Integer = 0 To mc.count - 1
        'msgbox(mc(i).Value)
        If mc(i).value = "FT" Then
            FTT=mc(i).value
            Exit For
        End If
    Next
    rsps.Close

catch ex As Exception
    'msgbox(ex.message)

End try
If FTT="FT" Then
'msgbox("内网IP通畅")

If e.name = "IBMDMAIN" Then
e.ConnectionString=(nwzip)
End If
If e.name = "IBMDLOGO" Then
e.ConnectionString=(nwLip)
End If

Else
Dim url2 As String = "http://222.241.172.223:8008/test"  '请按实际升级的路径填写
Dim FTT2 As String
try
    Dim rqst2 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url2)
    rqst2.TimeOut = 1000 '一秒
    Dim rsps2 As System.Net.HttpWebResponse = rqst2.GetResponse
    Dim stm2 As System.IO.Stream = rsps2.GetResponseStream()
    Dim reader2 As New System.IO.StreamReader(stm2)
    Dim str2 As String = reader2.ReadToEnd
   
    Dim mc2 = System.Text.RegularExpressions.Regex.Matches(str2, "(?<=\<BODY\>+.+;+).+(?=\</BODY?\>+)")
    For t As Integer = 0 To mc2.count - 1
        'msgbox(mc(i).Value)
        If mc2(t).value = "FT" Then
            FTT2=mc2(t).value
            Exit For
        End If
    Next
    rsps2.Close
catch ex As Exception
    'msgbox(ex.message)
End try
If FTT2="FT" Then
'msgbox("外网IP通畅")

If e.name = "IBMDMAIN" Then
e.ConnectionString=(wwzip)
End If

If e.name = "IBMDLOGO" Then
e.ConnectionString=(wwLip)

End If
Else
msgbox("网络不通,请联系网络管理员")
End If
End If

 

本方案关键点:

1在服务器制作一个test网页,网页内容为FT两字。 假设该网页能打开就表示服务器能正常使用,该网页能打开,而数据库刚好出现问题不能登陆的可能性比较少就不考虑了。

2在项目目录下制作 一个123.txt的iP地址配置文件。配置连接字符串可使用加密字符串。顺序本例为:内网主连接串,内网LOG连接串,外网主连接串,外网LOG连接串 。

3在路由器上要作8008等端口映射

4以上代码放在 BeforeConnectOuterDataSource 事件中。

 

这样程序在内网使用,就自动使用内网IP,在外见多 使用就自动使用外网IP,无需人工干预。

二连接配置字符串,由管理员制作发,分发给所有人放项目文件夹下面。加密存储,不暴露数据库之关键信息,提高数据安全。

 

遗憾的是没有做出多账套登陆,按此方法如果要作多账套登陆,我想到的办法是另做一个批理,自动改变配置文件123.txt的内容,从而达到开启程序自动登陆指定的数据库系统。


 回到顶部