Danh mục

Viewcode Website với VB

Số trang: 9      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 20      Lượt tải: 0    
Jamona

Phí tải xuống: 5,000 VND Tải xuống file đầy đủ (9 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Cách thực hiện: Sử dụng các hàm API InternetOpen,InternetReadFile... để tải nội dung từ 1 địa chỉ.Bạn xem code mẫu sau, trong đó hàm: Private Function InternetGetContent(sServerName As String, sFileName As String, Optional sUsername As String = vbNullString, Optional sPassword As String = vbNullString, Optional lBufferSize As Long = -1) As String Thực hiện việc download nội dung (html source code) từ file sFileName, đặt tại host: sServerName.
Nội dung trích xuất từ tài liệu:
Viewcode Website với VB Viewcode Website với VBCách thực hiện: Sử dụng các hàm API InternetOpen,InternetReadFile... để tảinội dung từ 1 địa chỉ.Bạn xem code mẫu sau, trong đó hàm:Private Function InternetGetContent(sServerName As String, sFileName AsString, Optional sUsername As String = vbNullString, Optional sPassword AsString = vbNullString, Optional lBufferSize As Long = -1) As StringThực hiện việc download nội dung (html source code) từ file sFileName, đặttại host: sServerName.Bạn tạo 1 ứng dụng mới, sau đó copy/paste đoạn code sau vào Form1 rồi chạythử nhé.Code:Option ExplicitPrivate Declare Function InternetOpen Lib wininet Alias InternetOpenA(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyNameAs String, ByVal sProxyBypass As String, ByVal lFlags As Long) As LongPrivate Declare Function InternetCloseHandle Lib wininet (ByVal hInet AsLong) As IntegerPrivate Declare Function InternetReadFile Lib wininet (ByVal hFile AsLong, ByVal sBuffer As String, ByVal lNumBytesToRead As Long,lNumberOfBytesRead As Long) As IntegerPrivate Declare Function InternetConnect Lib wininet.dll AliasInternetConnectA (ByVal hInternetSession As Long, ByVal sServerNameAs String, ByVal nServerPort As Integer, ByVal sUsername As String, ByValsPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVallContext As Long) As LongPrivate Declare Function HttpQueryInfo Lib wininet.dll AliasHttpQueryInfoA (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long,ByRef sBuffer As Any, ByRef lBufferLength As Long, ByRef lIndex AsLong) As IntegerPrivate Declare Function HttpOpenRequest Lib wininet.dll AliasHttpOpenRequestA (ByVal hHttpSession As Long, ByVal sVerb As String,ByVal sObjectName As String, ByVal sVersion As String, ByVal sReferer AsString, ByVal something As Long, ByVal lFlags As Long, ByVal lContext AsLong) As LongPrivate Declare Function HttpSendRequest Lib wininet.dll AliasHttpSendRequestA (ByVal hHttpRequest As Long, ByVal sHeaders AsString, ByVal lHeadersLength As Lo ng, sOptional As Any, ByVallOptionalLength As Long) As Integer--Private Function InternetGetContent(sServerName As String, sFileName AsString, Optional sUsername As String = vbNullString, Optional sPassword AsString = vbNullString, Optional lBufferSi ze As Long = -1) As String Dim hInternetSession As Long, hInternetConnect As Long,hHttpOpenRequest As Long Dim lRetVal As Long, lLenFile As Long, lNumberOfBytesRead As Long,lResLen As Long Dim sBuffer As String, lTotalBytesRead As Long Const clBufferIncrement As Long = 2000, scUserAgent As String =VBUsers Const INTERNET_OPEN_TYPE_PRECONFIG = 0,INTERNET_FLAG_EXISTING_CONNECT = &H20000000 Const INTERNET_OPEN_TYPE_DIRECT = 1,INTERNET_OPEN_TYPE_PROXY = 3 Const INTERNET_DEFAULT_HT TP_PORT = 80,INTERNET_FLAG_RELOAD = &H80000000 Const INTERNET_SERVICE_HTTP = 3 Const HTTP_QUERY_CONTENT_LENGTH = 5 If lBufferSize = -1 Then Create an arbitary buffer to read the whole file in parts sBuffer = String$(clBufferIncrement, vbNullChar) lBufferSize = clBufferIncrement Else Create a specified buffer size sBuffer = String$(lBufferSize, vbNullChar) End If Initializes an applications use of the Win32 Internet functions hInternetSession = InternetOpen(scUserAgent,INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) Opens an FTP, Gopher, or HTTP session for a given site hInternetConnect = InternetConnect(hInternetSession, sServerName,INTERNET_DEFAULT_HTTP_PORT, sUsername, sPassword,INTERNET_SERVICE_HTTP, 0, 0) Create an HTTP request handle hHttpOpenRequest = HttpOpenRequest(hInternetConnect, GET,sFileName, HTTP/1.0, vbNullString, 0, INTERNET_FLAG_RELOAD, 0) Creates a new HTTP request handle and store s the specified parameters inthat handle lRetVal = HttpSendRequest(hHttpOpenRequest, vbNullString, 0, 0, 0) If lRetVal Then Determine the file size lResLen = lBufferSize lRetVal = HttpQueryInfo(hHttpOpenRequest,HTTP_QUERY_CONTENT_LENGTH, ByVal sBuffer, lResLen, 0) If lRetVal Then Successfully returned file length lLenFile = Val(Left$(sBuffer, lResLen)) Create a buffer to hold file sBuffer = String$(lLenFile, vbNullChar) lBufferSize = lLenFile Else Unable to establish file length lLenFile = -1 End If Read the file Do lRetVal = InternetReadFile(hHttpOpenRequest, sBuffer, lBufferSize,lNumberOfBytesRead) Store the results InternetGetContent = InternetGetContent & Left$(sBuffer,lNumberOfBytesRead) lTotalBytesRead = lTotalBytesRead + lNumberOfBytesRead If lNumberOfBytesRead = 0 Or lTotalBytesRead = lLenFile Or lRetVal= 0 Then Finished reading file Exit Do End If Loop End If Close handles In ...

Tài liệu được xem nhiều: