如何打开提取网页中的ip地址。

xibeimu 2008-03-07 09:01:15
如何打开提取网页中的ip地址

用vb写了一个程序可以提取网页上的一个ip地址,现在想添加一个功能,提取后可以在新的窗口用ie连接打开这个ip地址。
现将提取网页的代码附上,请指教怎么才能直接打开这个地址。
其中text2中在点击 Command1后会提取到http://vbnet.mvps.org/resources/tools/getpublicip.shtml上的ip地址
我现在是想再加一个按钮使点击后直接连接到text2中提取的ip地址。
红色标注的是提取出ip的文件,
绿色是我写的打开那个地址的一段代码,但是不行希望有老师可以帮助一下。

一下是提取的代码是自动获得ip地址的原码
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecute A" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd A s Long) As Long
Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long



Private Sub Form_Load()

Command1.Caption = "Get Public IP"

Text1.Text = LocalIPAddress()
Text2.Text = ""

End Sub


Private Sub Command1_Click()

Text2.Text = GetPublicIP()


End Sub
Private Sub Command2_Click()

ShellExecute 0, "open", "????????", vbNullString, vbNullString, 3


End Sub



Private Function GetPublicIP()

Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
Dim buff As String
Dim pos1 As Long
Dim pos2 As Long

'site returning IP address
sSourceUrl = "http://vbnet.mvps.org/resources/tools/getpublicip.shtml"
sLocalFile = "c:\ip.txt"

'ensure this file does not exist in the cache
Call DeleteUrlCacheEntry(sSourceUrl)

'download the public IP file,
'read into a buffer and delete
If DownloadFile(sSourceUrl, sLocalFile) Then

hfile = FreeFile
Open sLocalFile For Input As #hfile
buff = Input$(LOF(hfile), hfile)
Close #hfile


'look for the IP line
pos1 = InStr(buff, "var ip =")

'if found,
If pos1 Then

'get position of first and last single
'quotes around address (e.g. '11.22.33.44')
pos1 = InStr(pos1 + 1, buff, "'", vbTextCompare) + 1
pos2 = InStr(pos1 + 1, buff, "'", vbTextCompare) '- 1

'return the IP address
GetPublicIP = Mid$(buff, pos1, pos2 - pos1)

Else

GetPublicIP = "(unable to parse IP)"

End If 'pos1

Kill sLocalFile

Else

GetPublicIP = "(unable to access shtml page)"

End If 'DownloadFile

End Function


Private Function DownloadFile(ByVal sURL As String, _
ByVal sLocalFile As String) As Boolean

DownloadFile = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = ERROR_SUCCESS

End Function


Private Function LocalIPAddress() As String

Dim cbRequired As Long
Dim buff() As Byte
Dim ptr1 As Long
Dim sIPAddr As String
Dim Adapter As IP_ADAPTER_INFO

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

'get a pointer to the data stored in buff()
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

'copy the data from the pointer to the
'first adapter into the IP_ADAPTER_INFO type
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'the DHCP IP address is in the
'IpAddress.IpAddr member

sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then Exit Do

ptr1 = .dwNext

End With 'With Adapter

'ptr1 is 0 when (no more adapters)
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'return any string found
LocalIPAddress = sIPAddr

End Function


Private Function TrimNull(startstr As String) As String

TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))

End Function

...全文
1338 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
没遇到过这种情况.
cbm6666 2008-03-09
  • 打赏
  • 举报
回复
'获取本机IP

Private Sub Command1_Click()
Dim winIP As Object
Set winIP = CreateObject("MSWinsock.Winsock")
MsgBox aa & "本机IP:" & winIP.LocalIP
End Sub



cbm6666 2008-03-09
  • 打赏
  • 举报
回复
Private Sub Command2_Click() '你的代码绿色部份
Text2.Text = "64.233.167.99" '你的代码中得到的IP
Shell "explorer http://" & Text2.Text, vbNormalFocus
'Shell "explorer " & "http://" & text2.text, vbNormalFocus
End Sub

cbm6666 2008-03-09
  • 打赏
  • 举报
回复
Dim Ipaddr$
Private Sub Command2_Click() '你的代码绿色部份
Ipaddr = "64.233.167.99" '你的代码中得到的IP
Shell "explorer http://" & Ipaddr, vbNormalFocus
'Shell "explorer " & "http://" & Ipaddr, vbNormalFocus
End Sub

xibeimu 2008-03-09
  • 打赏
  • 举报
回复
谢谢!
控件只能提取ip地址,我现在是想实现打开提取的ip地址
我先给你说一下我想实现的功能吧!

我已经实现了可以从网站上提取一个ip地址的功能了
我现在想通过添加一个按钮实现打开这个ip地址的功能
我不知道用什么方法请教了!



点获得登陆ip可以得到

现在想通过“登陆网站”按钮实现直接在IE中打开这个ip地址

有什么方法可以实现吗?

Text2.Text = GetPublicIP()
xujinzheng 2008-03-08
  • 打赏
  • 举报
回复
用 inet控件啊

dim a as string
a=inet1.openurl("http://vbnet.mvps.org/resources/tools/getpublicip.shtml")
msgbox a

其中a就是获得的IP地址
zzyong00 2008-03-08
  • 打赏
  • 举报
回复
要连接一个IP地址?什么意思?是用什么协议?
如:http:\\12.12.12.12
ftp:\\12.12.12.12

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧