POST獲得網上返回資源,是回論壇中一個貼子寫的
'=================================================
'
'posttofile("http://entry.126.com/cgi/login", _
' "<base href=""http://m134.mail.126.com/"">", _
' "user=XXX&pass=XXX", "XXX.html")
'
'Power by: Landlordh
'說明:
' 轉載請保留原有著作權資訊,請不要用於非法途徑
' 此函數沒有考慮相關的檔案夾許可權問題
'==================================================
Private Cookies As Net.CookieContainer = Nothing
Private Sub posttofile(ByVal HttpUrl As String, ByVal baseHref As String, ByVal poststr As String, ByVal FileName As String)
Try
Dim code As System.Text.Encoding = System.Text.Encoding.GetEncoding("GB18030") '"big5"
Dim Url As New System.Uri(HttpUrl)
Dim req As Net.HttpWebRequest = CType(Net.WebRequest.Create(Url), Net.HttpWebRequest)
Dim SomeByte() As Byte = code.GetBytes(poststr)
req.KeepAlive = False
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
'req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)"
req.ContentLength = SomeByte.Length
If Not (Me.Cookies Is Nothing) Then
req.CookieContainer = Me.Cookies
Else
req.CookieContainer = New Net.CookieContainer
Me.Cookies = req.CookieContainer
End If
Dim WriteStream As IO.Stream = req.GetRequestStream
WriteStream.Write(SomeByte, 0, SomeByte.Length)
WriteStream.Close()
Dim res As Net.HttpWebResponse = CType(req.GetResponse, Net.HttpWebResponse)
Dim receiveStream As IO.Stream = res.GetResponseStream
Dim ReadStream As StreamReader = New StreamReader(receiveStream, code)
Dim getstr As String
Dim read(256) As [Char]
Dim count As Integer = ReadStream.Read(read, 0, 256)
While count > 0
Dim str As New [String](read, 0, count)
getstr &= str
count = ReadStream.Read(read, 0, 256)
End While
ReadStream.Close()
receiveStream.Close()
req.Abort()
res.Close()
getstr = getstr.Insert(0, baseHref) '如果用了相對路徑,控制目標的base href,否則打不開網頁
Try
If FileName.Trim.Length = 0 Or FileName = Nothing Then
'不返迴文件
Else
If IO.File.Exists(FileName) Then IO.File.Delete(FileName)
Dim sr As New IO.StreamWriter(IO.File.Create(FileName), code)
'Dim sr As IO.StreamWriter = IO.File.CreateText(FileName) '編碼相容(UTF8)
sr.Write(getstr)
sr.Close()
End If
Catch e As Exception
MsgBox(e.Message)
End Try
Catch
End Try
End Sub