none
大伙看看这段代码哪里不对,在VB6中可以,在VBnet中就不行了 RRS feed

  • 问题

  • Public Class Form1
        Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
        Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
        Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, ByVal lNumberOfBytesRead As Long) As Integer
        Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
        Private Const IF_FROM_CACHE = &H1000000
        Private Const IF_MAKE_PERSISTENT = &H2000000
        Private Const IF_NO_CACHE_WRITE = &H4000000
        Private Const BUFFER_LEN = 256

      
     Public Function GetUrlSource(ByVal sURL As String) As String
            Dim sBuffer, sData As String
            Dim BUFFER_LEN, iResult As Integer
            Dim hInternet As Long, hSession As Long, lReturn As Long
            hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
            If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
            If hInternet Then
                iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
                sData = sBuffer
                Do While lReturn <> 0
                    iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
                    sData = sData + Mid(sBuffer, 1, lReturn)
                Loop
            End If
            If InStr(1, sData, Chr(0)) Then sData = Mid(sData, 1, InStr(1, sData, Chr(0)) - 1)
            iResult = InternetCloseHandle(hInternet)
            GetUrlSource = sData
        End Function
      

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = GetUrlSource("http://www.baidu.com")
        End Sub
    End Class

    2011年2月7日 8:52

答案

全部回复