[原創]POST獲得網上返回資源

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.