dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

  1. #1
    Join Date
    Sep 2018
    Posts
    3

    ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    I have the following problem. I save a radar image file from a web page. the file always has the same filename. That's why I have to investigate if a new image exists or not.
    Here is my code:

    Code:
    Option Explicit
     
    Private sLastModified As String
    
    Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000
    Const INTERNET_FLAG_ASYNC = &H10000000
    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
    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
    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As _
        Long) As Integer
    Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As _
        Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, _
        lNumberOfBytesRead As Long) As Integer
        
        
        Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" _
      Alias "DeleteUrlCacheEntryA" ( _
      ByVal lpszUrlName As String) As Long
    Private Sub Command1_Click()
    
    Dim URL As String
    Dim mydateTime
    
    
    Dim http        As MSXML2.XMLHTTP
    
        Set http = New MSXML2.XMLHTTP
    
    
    URL = "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg"
    Label3.Caption = Label1.Caption
    Label4.Caption = Label2.Caption
    Call ConditionallyRetrieveEntity("https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg", Label1.Caption, Label2.Caption)
    
    
           
           
    End Sub
    
    Public Function ConditionallyRetrieveEntity(URL As String, eTag As String, ifModifiedSince As String)
     Dim http As MSXML2.XMLHTTP
    Dim bytes()     As Byte
    
    
    
    Dim mydateTime
    Dim noMatch
    Dim D1
    
    'If URL <= "" Then
        'Exit Sub
    'End If
    Call ReadResponse
    URL = "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg"
     
        Set http = New MSXML2.XMLHTTP
        http.open "GET", "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg", False
        http.setRequestHeader "If-None-Match", eTag
        'http.setRequestHeader "If-Modified-Since", ifModifiedSince
        http.send
     
     Text1.Text = http.getAllResponseHeaders
     Label6.Caption = http.Status
     
     Select Case http.Status
        Case 200
            mydateTime = FormatDateTime(Mid$(Label2.Caption, 6, 20), vbGeneralDate)
           mydateTime = Replace(mydateTime, "/", "")
           mydateTime = Replace(mydateTime, ":", "_")
        
             Call CopyURLToFile(URL, "D:\PP\XMLHTTP Check ETag and Time\Turkey Pics\Turkey" & mydateTime & ".jpg")
        Case 304
            Exit Function
    End Select
        
    End Function
    
    Public Function ReadResponse()
     Dim http As MSXML2.XMLHTTP
    Dim bytes()     As Byte
    Dim URL As String
    Dim eTag As String
    Dim ifModifiedSince As String
    Dim mydateTime
    Dim noMatch
    Dim D1
    URL = "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg"
     
        Set http = New MSXML2.XMLHTTP
        http.open "GET", "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg", False
        http.send
        eTag = http.getResponseHeader("ETag")
        ifModifiedSince = http.getResponseHeader("Last-Modified")
        
        Label1.Caption = eTag
        Label2.Caption = ifModifiedSince
    End Function
    
    
    Private Sub Timer1_Timer()
    Dim URL As String
    Dim mydateTime
    
    
    Dim http        As MSXML2.XMLHTTP
    
        Set http = New MSXML2.XMLHTTP
    
    
    URL = "https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg"
    Label3.Caption = Label1.Caption
    Label4.Caption = Label2.Caption
    Call ConditionallyRetrieveEntity("https://mgm.gov.tr/FTPDATA/uzal/radar/comp/compppi15.jpg", Label1.Caption, Label2.Caption)
    End Sub
    
    Sub CopyURLToFile(ByVal URL As String, ByVal FileName As String)
        Dim hInternetSession As Long
        Dim hUrl As Long
        Dim FileNum As Integer
        Dim ok As Boolean
        Dim NumberOfBytesRead As Long
        Dim Buffer As String
        Dim fileIsOpen As Boolean
    
        On Error GoTo ErrorHandler
    
        ' check obvious syntax errors
        If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5
    
        ' open an Internet session, and retrieve its handle
        hInternetSession = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, _
            vbNullString, vbNullString, 0)
        If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , _
            "An error occurred calling InternetOpen function"
    
        ' open the file and retrieve its handle
        hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0, _
            INTERNET_FLAG_EXISTING_CONNECT, 0)
        If hUrl = 0 Then Err.Raise vbObjectError + 1000, , _
            "An error occurred calling InternetOpenUrl function"
    
        ' ensure that there is no local file
        On Error Resume Next
        Kill FileName
    
        On Error GoTo ErrorHandler
        
        ' open the local file
        FileNum = FreeFile
        Open FileName For Binary As FileNum
        fileIsOpen = True
    
        ' prepare the receiving buffer
        Buffer = Space(4096)
        
        Do
            ' read a chunk of the file - returns True if no error
            ok = InternetReadFile(hUrl, Buffer, Len(Buffer), NumberOfBytesRead)
    
            ' exit if error or no more data
            If NumberOfBytesRead = 0 Or Not ok Then Exit Do
            
            ' save the data to the local file
            Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
        Loop
        
        ' flow into the error handler
    
    ErrorHandler:
        ' close the local file, if necessary
        If fileIsOpen Then Close #FileNum
        ' close internet handles, if necessary
        If hUrl Then InternetCloseHandle hUrl
        If hInternetSession Then InternetCloseHandle hInternetSession
        
        ' report the error to the client, if there is one
        If Err Then Err.Raise Err.Number, , Err.Description
    
    End Sub


    The problem is that I can not rely on it. sometimes the program tells me that there is a new image but it is still the same image as before.
    Maybe someone can help me to solve this problem.
    thanks in advance and greetings.
    Last edited by 2kaud; September 14th, 2018 at 09:55 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,516

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#']

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,872

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    Your Standard Filename is compppi15.jpg

    Why not just Save the Downloaded File name as a Number "Downloads Directory"

    You then end up with a Directory with
    100001.jpg
    100002.jpg
    100003.jpg
    100004.jpg
    100005.jpg

    etc

    The last file is the last file you downloaded, right ?

    If 100005 is the same as 100004 then what is the problem ?

    Presumably the file size is always the same size, otherwise you can check the sizes of files downloaded

    Does the DateTime of the File Downloaded not help ?

  4. #4
    Join Date
    Sep 2018
    Posts
    3

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    Hi George1111,
    first thanks for your reply!
    I think your suggestion will not help me so much.
    the radar image has a map background and if there is no precipitation the files has all the same size.
    I have to check if a new image is present or not.
    So somehow with last-modified or if-none-match or if-unmodified.

    Thank you very much and greetings

  5. #5
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,872

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    Quote Originally Posted by carlo2013 View Post
    Hi George1111,
    first thanks for your reply!
    I think your suggestion will not help me so much.
    the radar image has a map background and if there is no precipitation the files has all the same size.
    I have to check if a new image is present or not.
    So somehow with last-modified or if-none-match or if-unmodified.

    Thank you very much and greetings

    Ah, so the image is a RAIN Image !
    ie, There is a lot of Rain or there is a little Rain or there is no Rain - right ?

    So if you download an image every day, for 1 week, there will be 7 images if you only down an image once a day
    OR
    Do you want to download an image, and publish it ONLY if it Changes, which could be several hundred times a day if you have intermittent showers during the day ?

    Is there a possibility that the image was not taken correctly, and therefore you dont want to publish it ?
    This is the case that the previous image is the same as the last image because the last image is actually the previous image.

    In this case, if you do publish the new image download, and it is actually the same as the previous image shown, then what is the problem ?
    (You are just looking at exactly the same image anyway)

    If we go to great lengths to discover that the last image is actually the previous image then the person seeing the image
    will just see the same thing

    OR

    Do you want alarm bells to sound which warn you that the imaging system has failed, and is not updating images correctly,
    and someone needs to climb up on a tower to fix it ?

    Imagine if you download 60 images an hour (1 per minute) and some of them don't quite meet your standard, how will this affect what you are attempting to achieve ?
    Imagine if you download 600 images an hour (10 per minute) and some of them don't quite meet your standard, how will this affect what you are attempting to achieve ?
    Imagine if you download images continuously and some of them don't quite meet your standard, how will this affect what you are attempting to achieve ?

  6. #6
    Join Date
    Sep 2018
    Posts
    3

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    Hi george 1111
    Sorry for the very late reply. But I was on vacation.
    So now to your questions.
    The file always has the same name. But the radar picture is not the same.
    New files will be created every 5-6 minutes.
    I want to save all radar images that are not duplicate. So whenever a new one is posted on the webpage.
    In addition, I need the last-modified time to create a daily folder and store the image there, with year, month, day and time.
    Storing duplicate images is not really an option.
    Thanks in advance and greetings.

  7. #7
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,872

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    Quote Originally Posted by carlo2013 View Post
    Hi george 1111
    Sorry for the very late reply. But I was on vacation.
    So now to your questions.
    The file always has the same name. But the radar picture is not the same.
    New files will be created every 5-6 minutes.
    I want to save all radar images that are not duplicate. So whenever a new one is posted on the webpage.
    In addition, I need the last-modified time to create a daily folder and store the image there, with year, month, day and time.
    Storing duplicate images is not really an option.
    Thanks in advance and greetings.
    This may help ...
    Search for "vb6 how to check if one image is the same as another" in Google

    The first hit in VBForums ("Determining if two pictures are identical-VBForums") suggests you load the Last Image into a PictureBox and the Previous Image into another. Then, by using the GetPixel command you can check if the images are the same or not.

    A Quicker check may be to compare the files byte by byte. see "Compare Two Files to Determine if They are Identical - FreeVBCode"

    I haven't looked deeply into these solution but they just may be what you are looking for

    All the best
    George

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,302

    Re: ETag ? Last-Modified-Since ? If-unmodified-Since ? ...

    My first thought is to shell FC [the old Dos File Compare] and check the result. It should be very fast and will tell you if the files are the same or not. I've never looked to see if there is a newer API equivalent. Been using FC for ages.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)