Link to home
Start Free TrialLog in
Avatar of vbkann
vbkann

asked on

Http - wininet problem

I use a class module to recieve files off the internet. The files that i am recieving are always changing the data contained within them.

I am recieving .txt files and values returned by .cgi files.

However, the values that i get returned to me in code are values from the cache and not what the files contain.

I use the INTERNET_FLAG_RELOAD constant in the following code but i still get values returned that are not correct:
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", UrlAddress, "HTTP/1.0", vbNullString, 0, INTERNET_FLAG_RELOAD, 0)

What is going wrong and how can i fix it please.

thanks
Avatar of setiawan
setiawan

see www.vbip.com
Hope this helps you

 regards

  danny
Avatar of vbkann

ASKER

Is there a specific piece of code there that i should look at? If so could you give me the url to its page as i cannot find anything there to help me really. Sorry
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try also internet_flag_pragma_nocache
Hi vbkann,

I'm sorry, the links not about HTTP using WinInet API, but using Winsock.

Who knows, you want to try winsock
http://www.vbip.com/winsock/winsock_http_01.asp

  regards

   danny
I am not sure, but I think the the reload only works with the InternetOpenUrl call.  You will need to rem out some of the error stuff below to get this to work, but it is very solid.  

Here is the call to open the internet session:
hInternetSession = InternetOpen(sUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)

Here is the function:
Public Function ReadUrl(ByVal sURL As String, Optional vFileName As Variant) As Boolean
Dim sReadBuffer         As String * 2048 ' bytes to read from call to InternetReadFile
Dim lNumberOfBytesRead  As Long         ' bytes read from call to InternetReadFile
Dim lTotalBytesRead     As Long         ' total bytes read
Dim bDoLoop             As Boolean      ' return value from InternetReadFile
Dim bReadInternetFile   As Boolean
Dim bWriteToFile        As Boolean
On Error GoTo errReadUrl
Screen.MousePointer = vbHourglass
SetStatus "Opening: " & sURL
If Not IsMissing(vFileName) Then
    Dim iFileNum As Integer
    iFileNum = FreeFile
    Open CStr(vFileName) For Binary As iFileNum
    bWriteToFile = True
End If
Dim dwFlags As Long
dwFlags = INTERNET_FLAG_EXISITING_CONNECT
dwFlags = dwFlags + INTERNET_FLAG_RELOAD + INTERNET_FLAG_NO_COOKIES
hUrlFile = InternetOpenUrl(hInternetSession, sURL, vbNullString, 0, dwFlags, 0)
If CBool(hUrlFile) Then
    sContents = scBlankStr
    bDoLoop = True
    While bDoLoop
        sReadBuffer = scBlankStr
        bDoLoop = InternetReadFile(hUrlFile, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
        If Not CBool(bDoLoop) Then CheckError
        lTotalBytesRead = lTotalBytesRead + lNumberOfBytesRead
        SetStatus "Reading Url: " & CStr(lTotalBytesRead) & " bytes read"
        If CBool(lNumberOfBytesRead) Then
            If bWriteToFile Then
                Put #iFileNum, , sReadBuffer
            Else
                sContents = sContents & Left$(sReadBuffer, lNumberOfBytesRead)
            End If
        Else
            bDoLoop = False
            bReadInternetFile = True
        End If
    Wend
    InternetCloseHandle (hUrlFile)
    If bWriteToFile Then Close
    ReadUrl = True
Else
    CheckError
End If
SetStatus "Ready"
Screen.MousePointer = vbDefault
Exit Function
errReadUrl:
sLastError = Error$(Err)
Screen.MousePointer = vbDefault
Exit Function
End Function
Avatar of vbkann

ASKER

ill try the internet_flag_pragma_nocache and internet_flag_resynchronize flags, but what are the values for these constants?
Avatar of vbkann

ASKER

what are the values of all the http constants actually?
good question.  microsoft provides no documentation i can find showing the declarations for these constants. damn ms!  i will try to look on the web for some project examples that use the wininet api to see if they have the constants declared in them.  the info has to be out there somewhere.
Private Type INTERNET_VERSION_INFO
    dwMajorVersion As Long
    dwMinorVersion  As Long
End Type

Private Type HTTP_VERSION_INFO
    dwMajorVersion As Long
    dwMinorVersion  As Long
End Type

Private Const INTERNET_INVALID_PORT_NUMBER = 0             ' use the protocol-specific default
Private Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
Private Const INTERNET_DEFAULT_GOPHER_PORT = 70            '    "     "  gopher "
Private Const INTERNET_DEFAULT_HTTP_PORT = 80              '    "     "  HTTP   "
Private Const INTERNET_DEFAULT_HTTPS_PORT = 443            '    "     "  HTTPS  "
Private Const INTERNET_DEFAULT_SOCKS_PORT = 1080           ' default for SOCKS firewall servers.

' maximum field lengths (arbitrary)
Private Const INTERNET_MAX_HOST_NAME_LENGTH = 256
Private Const INTERNET_MAX_USER_NAME_LENGTH = 128
Private Const INTERNET_MAX_PASSWORD_LENGTH = 128
Private Const INTERNET_MAX_PORT_NUMBER_LENGTH = 5          ' INTERNET_PORT is unsigned short
Private Const INTERNET_MAX_PORT_NUMBER_VALUE = 65535       ' maximum unsigned short value
Private Const INTERNET_MAX_PATH_LENGTH = 2048
Private Const INTERNET_MAX_SCHEME_LENGTH = 32              ' longest protocol name length
'Private Const INTERNET_MAX_URL_LENGTH        (INTERNET_MAX_SCHEME_LENGTH \
'                                        + sizeof("://") \
'                                        + INTERNET_MAX_PATH_LENGTH)

' values returned by InternetQueryOption() with INTERNET_OPTION_KEEP_CONNECTION:
'Private Const INTERNET_KEEP_ALIVE_UNKNOWN     ((DWORD)-1)
Private Const INTERNET_KEEP_ALIVE_ENABLED = 1
Private Const INTERNET_KEEP_ALIVE_DISABLED = 0

' flags returned by InternetQueryOption() with INTERNET_OPTION_REQUEST_FLAGS
Private Const INTERNET_REQFLAG_FROM_CACHE = &H1             ' response came from cache
Private Const INTERNET_REQFLAG_ASYNC = &H2                  ' request was made asynchronously
Private Const INTERNET_REQFLAG_VIA_PROXY = &H4              ' request was made via a proxy
Private Const INTERNET_REQFLAG_NO_HEADERS = &H8             ' orginal response contained no headers
Private Const INTERNET_REQFLAG_PASSIVE = &H10               ' FTP: passive-mode connection
Private Const INTERNET_REQFLAG_CACHE_WRITE_DISABLED = &H40        ' HTTPS: this request not cacheable

' flags common to open functions (not InternetOpen()):
Private Const INTERNET_FLAG_RELOAD = &H80000000             ' retrieve the original item

' flags for InternetOpenUrl():
Private Const INTERNET_FLAG_RAW_DATA = &H40000000           ' FTP/gopher find: receive the item as raw (structured) data
Private Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000   ' FTP: use existing InternetConnect handle for server if possible

' flags for InternetOpen():
Private Const INTERNET_FLAG_ASYNC = &H10000000              ' this request is asynchronous (where supported)

' protocol-specific flags:
Private Const INTERNET_FLAG_PASSIVE = &H8000000             ' used for FTP connections

' additional cache flags
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000     ' don't write this item to the cache
Private Const INTERNET_FLAG_DONT_CACHE = INTERNET_FLAG_NO_CACHE_WRITE
Private Const INTERNET_FLAG_MAKE_PERSISTENT = &H2000000     ' make this item persistent in cache
Private Const INTERNET_FLAG_FROM_CACHE = &H1000000          ' use offline semantics
Private Const INTERNET_FLAG_OFFLINE = INTERNET_FLAG_FROM_CACHE

' additional flags
Private Const INTERNET_FLAG_SECURE = &H800000               ' use PCT/SSL if applicable (HTTP)
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000      ' use keep-alive semantics
Private Const INTERNET_FLAG_NO_AUTO_REDIRECT = &H200000     ' don't handle redirections automatically
Private Const INTERNET_FLAG_READ_PREFETCH = &H100000        ' do background read prefetch
Private Const INTERNET_FLAG_NO_COOKIES = &H80000            ' no automatic cookie handling
Private Const INTERNET_FLAG_NO_AUTH = &H40000               ' no automatic authentication handling
Private Const INTERNET_FLAG_CACHE_IF_NET_FAIL = &H10000     ' return cache file if net request fails
 
 ' Security Ignore Flags, Allow HttpOpenRequest to overide
'  Secure Channel (SSL/PCT) failures of the following types.
Private Const INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP = &H8000       ' ex: https:' to http://
Private Const INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS = &H4000      ' ex: http:' to https://
Private Const INTERNET_FLAG_IGNORE_CERT_DATE_INVALID = &H2000      ' expired X509 Cert.
Private Const INTERNET_FLAG_IGNORE_CERT_CN_INVALID = &H1000        ' bad common name in X509 Cert.
 
' more caching flags
Private Const INTERNET_FLAG_RESYNCHRONIZE = &H800           ' asking wininet to update an item if it is newer
Private Const INTERNET_FLAG_HYPERLINK = &H400               ' asking wininet to do hyperlinking semantic which works right for scripts
Private Const INTERNET_FLAG_NO_UI = &H200                   ' no cookie popup
Private Const INTERNET_FLAG_PRAGMA_NOCACHE = &H100          ' asking wininet to add "pragma: no-cache"
Private Const INTERNET_FLAG_CACHE_ASYNC = &H80              ' ok to perform lazy cache-write
Private Const INTERNET_FLAG_FORMS_SUBMIT = &H40             ' this is a forms submit
Private Const INTERNET_FLAG_NEED_FILE = &H10                ' need a file for this request
Private Const INTERNET_FLAG_MUST_CACHE_REQUEST = INTERNET_FLAG_NEED_FILE
 
 ' manifests
Private Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const FTP_TRANSFER_TYPE_BINARY = &H2
 
' flags for FTP
Private Const INTERNET_FLAG_TRANSFER_ASCII = FTP_TRANSFER_TYPE_ASCII       ' = &H00000001
Private Const INTERNET_FLAG_TRANSFER_BINARY = FTP_TRANSFER_TYPE_BINARY     ' = &H00000002
 
 ' common per-API flags (new APIs)
Private Const WININET_API_FLAG_ASYNC = &H1                  ' force async operation
Private Const WININET_API_FLAG_SYNC = &H4                   ' force sync operation
Private Const WININET_API_FLAG_USE_CONTEXT = &H8            ' use value supplied in dwContext (even if 0)

' INTERNET_NO_CALLBACK - if this value is presented as the dwContext parameter
' then no call-backs will be made for that API
Private Const INTERNET_NO_CALLBACK = 0

' INTERNET_SCHEME - enumerated URL scheme type
Public Enum INTERNET_SCHEME
    INTERNET_SCHEME_PARTIAL = -2
    INTERNET_SCHEME_UNKNOWN = -1
    INTERNET_SCHEME_DEFAULT = 0
    INTERNET_SCHEME_FTP = 1
    INTERNET_SCHEME_GOPHER = 2
    INTERNET_SCHEME_HTTP = 3
    INTERNET_SCHEME_HTTPS = 4
    INTERNET_SCHEME_FILE = 5
    INTERNET_SCHEME_NEWS = 6
    INTERNET_SCHEME_MAILTO = 7
    INTERNET_SCHEME_SOCKS = 8
    INTERNET_SCHEME_FIRST = INTERNET_SCHEME_FTP
    INTERNET_SCHEME_LAST = INTERNET_SCHEME_SOCKS
End Enum



' flags for InternetCanonicalizeUrl() and InternetCombineUrl()
Private Const ICU_NO_ENCODE = &H20000000    ' Don't convert unsafe characters to escape sequence
Private Const ICU_DECODE = &H10000000       ' Convert %XX escape sequences to characters
Private Const ICU_NO_META = &H8000000       ' Don't convert .. etc. meta path sequences
Private Const ICU_ENCODE_SPACES_ONLY = &H4000000   ' Encode spaces only
Private Const ICU_BROWSER_MODE = &H2000000  ' Special encode/decode rules for browser
 
Private Declare Function InternetCanonicalizeUrl Lib "wininet.dll" Alias "InternetCanonicalizeUrlA" (ByVal lpszUrl As String, ByVal lpszBuffer As String, lpdwBufferLength As Long, ByVal dwFlags As Long) As Long

Private Declare Function InternetCombineUrl Lib "wininet.dll" Alias "InternetCombineUrlA" (ByVal lpszBaseUrl As String, ByVal lpszRelativeUrl As String, ByVal lpszBuffer As String, lpdwBufferLength As Long, ByVal dwFlags As Long) As Long


' access types for InternetOpen()
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0                     ' use registry configuration
Private Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
Private Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
Private Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS

' old names for access types
Private Const PRE_CONFIG_INTERNET_ACCESS = INTERNET_OPEN_TYPE_PRECONFIG
Private Const LOCAL_INTERNET_ACCESS = INTERNET_OPEN_TYPE_DIRECT
Private Const CERN_PROXY_INTERNET_ACCESS = INTERNET_OPEN_TYPE_PROXY

' opens and initializes an internet connection
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long

' closes a internet connection or URL handle
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

' opens a URL
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

' reads data from a URL
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

' INTERNET_ASYNC_RESULT - this structure is returned to the application via
' the callback with INTERNET_STATUS_REQUEST_COMPLETE. It is not sufficient to
' just return the result of the async operation. If the API failed then the
' app cannot call GetLastError() because the thread context will be incorrect.
' Both the value returned by the async API and any resultant error code are
' made available. The app need not check dwError if dwResult indicates that
' the API succeeded (in this case dwError will be ERROR_SUCCESS)

Private Type INTERNET_ASYNC_RESULT
    ' dwResult - the HINTERNET, DWORD or BOOL return code from an async API
    dwResult As Long
   
    ' dwError - the error code if the API failed
    dwError  As Long
End Type

Private Const INTERNET_FLAG_EXISITING_CONNECT = &H20000000
Private Const scTagDelimiter = """>?#"           ' default delimiter when scanning HTML
Private Const scBlankStr = ""                    ' blank string constant

Private hInternetSession    As Long     ' internet session handle
Private bInitialized        As Boolean  ' object initialization flag
Private hUrlFile            As Long     ' utl handle
Private sContents           As String   ' html page contents
Private sLastError          As String   ' last dll error buffer
Private sStatus             As String   ' object status buffer
Private objWindow           As Object   ' status message window
Private sUserAgent          As String   ' user agent in HTTP protocol

Avatar of vbkann

ASKER

Private Const INTERNET_FLAG_RESYNCHRONIZE = &H800, this does the trick thanks.

Also cheers for those other http constants.