程序中有时要用到查询IP地址归属地的问题,在网上查了一下,大部分是使用IP地址库来实现,但IP地址库的更新不是实时,通过对一些IP地址归属地查询网页的分析,采用Webservice的方式实现出来,免除了自已更新IP地址库问题!
- Imports System.Web
- Imports System.Web.Services
- Imports System.Web.Services.Protocols
- Imports System.Net
- <WebService(Namespace:="http://tempuri.org/")> _
- <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
- <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
- Public Class QueryService
- Inherits System.Web.Services.WebService
- ''' <summary>
- '''
- ''' </summary>
- ''' <param
- <WebMethod()> Public Function QueryIP(ByVal ip As String) As String
- Dim uri As New Uri("http://www.123cha.com/ip/?q=" + ip)
- Dim req As WebRequest = WebRequest.Create(uri)
- Dim sm As IO.Stream = req.GetResponse.GetResponseStream
- Dim sr As New IO.StreamReader(sm, System.Text.Encoding.Default)
- Dim html As String = sr.ReadToEnd
- Dim resultStart, resultStop As Integer
- Dim result As String
- resultStart = html.IndexOf("本站主数据</font>: ") + "本站主数据</font>: ".Length
- resultStop = html.IndexOf("<", resultStart)
- result = html.Substring(resultStart, resultStop - resultStart)
- Return result.Replace(" ", "")
- End Function
- End Class