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 Sub PostBackToFile(ByVal url As String, ByVal baseHref As String, ByVal poststr As String, ByVal FileName As String)
Try
Dim code As String = "GB18030" '"big5"
Dim req As Net.HttpWebRequest = CType(Net.WebRequest.Create(url), Net.HttpWebRequest)
Dim SomeByte() As Byte = System.Text.Encoding.ASCII.GetBytes(poststr)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = SomeByte.Length
req.GetRequestStream.Write(SomeByte, 0, SomeByte.Length)
Dim res As Net.HttpWebResponse = CType(req.GetResponse, Net.HttpWebResponse)
Dim ReadStream As StreamReader = New StreamReader(res.GetResponseStream, System.Text.Encoding.GetEncoding(code))
Dim strLine As String
Dim getstr As String
Do
strLine = ReadStream.ReadLine '用原来的排版
getstr &= strLine & vbCrLf
Loop Until strLine Is Nothing
ReadStream.Close()
getstr = getstr.Insert(0, baseHref) '如果用了相对路径,控制目标的base href,否则打不开网页
Try
If IO.File.Exists(FileName) Then IO.File.Delete(FileName)
Dim sr As New IO.StreamWriter(IO.File.Create(FileName), System.Text.Encoding.GetEncoding(code))
'Dim sr As IO.StreamWriter = IO.File.CreateText(FileName) '编码兼容(utf8)
sr.Write(getstr)
sr.Close()
Catch e As Exception
MsgBox(e.Message)
End Try
Catch
End Try
End Sub