查询手机归属地
Code
Imports System.Net
Imports System.io
Public Class Form1
Dim sessionid As String = "aa"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim url As String = "http://www.chinamobile.com/service/online/location.do"
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
sessionid = myHttpWebResponse.Headers.Get("Set-Cookie").Split(";")(0)
txtCont.Text = sessionid
myHttpWebResponse.Close()
myHttpWebRequest = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
GetHtmlA(txtName.Text)
End Sub
Sub GetHtmlA(ByVal mobile As String)
Dim url As String = "http://www.chinamobile.com/service/online/location.do"
Dim _client As WebClient = New WebClient()
_client.BaseAddress = url
_client.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
_client.Headers(HttpRequestHeader.Cookie) = sessionid
_client.Headers(HttpRequestHeader.Referer) = url
Dim postdata As Byte() = System.Text.Encoding.ASCII.GetBytes("sectionNum=" & mobile & "&urlType=1")
Dim responsedata As Byte() = _client.UploadData(url, "POST", postdata)
Dim temp As String = System.Text.Encoding.Default.GetString(responsedata)
_client.Dispose()
_client = Nothing
txt(temp)
End Sub
Sub txt(ByVal strCont As String)
Try
Dim i(2) As String
strCont = strCont.Remove(0, strCont.IndexOf(">手机号码:</TH>")).Replace(">手机号码:</TH>", "")
i(0) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
strCont = strCont.Remove(0, strCont.IndexOf(">所属省份:</TH>")).Replace(">所属省份:</TH>", "")
i(1) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
strCont = strCont.Remove(0, strCont.IndexOf(">所属城市:</TH>")).Replace(">所属城市:</TH>", "")
i(2) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
txtCont.Text = Trim(i(0) & "," & i(1) & "," & i(2)).Replace(vbTab, "").Replace(Chr(10), "").Replace(Chr(13), "")
Catch ex As Exception
End Try
End Sub
End Class
Imports System.Net
Imports System.io
Public Class Form1
Dim sessionid As String = "aa"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim url As String = "http://www.chinamobile.com/service/online/location.do"
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
sessionid = myHttpWebResponse.Headers.Get("Set-Cookie").Split(";")(0)
txtCont.Text = sessionid
myHttpWebResponse.Close()
myHttpWebRequest = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
GetHtmlA(txtName.Text)
End Sub
Sub GetHtmlA(ByVal mobile As String)
Dim url As String = "http://www.chinamobile.com/service/online/location.do"
Dim _client As WebClient = New WebClient()
_client.BaseAddress = url
_client.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
_client.Headers(HttpRequestHeader.Cookie) = sessionid
_client.Headers(HttpRequestHeader.Referer) = url
Dim postdata As Byte() = System.Text.Encoding.ASCII.GetBytes("sectionNum=" & mobile & "&urlType=1")
Dim responsedata As Byte() = _client.UploadData(url, "POST", postdata)
Dim temp As String = System.Text.Encoding.Default.GetString(responsedata)
_client.Dispose()
_client = Nothing
txt(temp)
End Sub
Sub txt(ByVal strCont As String)
Try
Dim i(2) As String
strCont = strCont.Remove(0, strCont.IndexOf(">手机号码:</TH>")).Replace(">手机号码:</TH>", "")
i(0) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
strCont = strCont.Remove(0, strCont.IndexOf(">所属省份:</TH>")).Replace(">所属省份:</TH>", "")
i(1) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
strCont = strCont.Remove(0, strCont.IndexOf(">所属城市:</TH>")).Replace(">所属城市:</TH>", "")
i(2) = Trim(strCont.Substring(0, strCont.IndexOf("</TD>"))).Replace("<TD >", "")
txtCont.Text = Trim(i(0) & "," & i(1) & "," & i(2)).Replace(vbTab, "").Replace(Chr(10), "").Replace(Chr(13), "")
Catch ex As Exception
End Try
End Sub
End Class
不过现在该网页加了图片验证码,所以此段代码不能成功查询,需要先破解验证码。待续