Results 1 to 21 of 21

Thread: ASP Remote file size

  1. #1

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    ASP Remote file size

    I am trying to get the filesize of a remote file, while acquiring as little as possible data, and failing miserably. The best I can think of is sending a HEAD request(using XMLHTTP), then getting the Content-Length header, but the site I'm contacting does not send Content-Length at all, so this fails.

    Can anyone help me with some other way to acomplish this?

    Thanks in advance for any help you might offer.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  2. #2
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: ASP Remote file size

    does the site in question belong to you?
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  3. #3

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Neither to me nor to my client.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  4. #4
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: ASP Remote file size

    What method are you using to send the request?
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  5. #5

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    I've tried GET, HEAD and OPTIONS(just in case). None return Content-Length.

    EDIT: To clarify, it's a text/html page, not a binary file.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  6. #6
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: ASP Remote file size

    it could be the version of IIS it's operating, or it's a weird server
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  7. #7
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    PM me the link and ill check its headers ..

  8. #8
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: ASP Remote file size

    I think i have a way using API, i'm just finalising the code
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  9. #9

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Here's an example from their site(automatically generated rss/xml): http://www.buyaphoto.net/rss/rss_gen3.php?&parent=2. They're using Apache BTW.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  10. #10
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: ASP Remote file size

    Right i've cracked it

    Tested it on a couple of pages, it works fine.

    VB Code:
    1. Option Explicit
    2.  
    3. Const scUserAgent = "IE 6.0"
    4. Const INTERNET_OPEN_TYPE_DIRECT = 1
    5. Const INTERNET_OPEN_TYPE_PROXY = 3
    6. Const INTERNET_FLAG_RELOAD = &H80000000
    7.  
    8. Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    9. Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
    10. Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    11. Private Declare Function InternetOpenUrl Lib "wininet" 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
    12.  
    13. Function GetFileLength(sUrl As String)
    14.  
    15.  
    16.     Dim hOpen As Long
    17.     Dim hFile As Long
    18.     Dim sBuffer As String
    19.     Dim Ret As Long
    20.     Dim oldret As Long
    21.     Dim oldbuffer As String
    22.     Dim bytesreadint As Long
    23.     'Create a buffer for the file we're going to download
    24.     sBuffer = Space(1000)
    25.     'Create an internet connection
    26.     hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    27.     'Open the url
    28.     hFile = InternetOpenUrl(hOpen, sUrl, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
    29.     'Read a1000 bytes of the file
    30.     oldbuffer = ""
    31.  
    32.     Do
    33.     oldbuffer = sBuffer
    34.        InternetReadFile hFile, sBuffer, 1000, Ret
    35.    
    36.     If Ret <> 1000 Then  'ret will not return 1000 if it cant i.e if there are less bytes than that left, instead, it will return the amount left.
    37.       bytesreadint = bytesreadint + Ret
    38.       Exit Do
    39.     Else
    40.     bytesreadint = bytesreadint + 1000
    41.     End If
    42.    
    43.    
    44.     Loop
    45.      
    46.    
    47.     'clean up
    48.     InternetCloseHandle hFile
    49.     InternetCloseHandle hOpen
    50.     'Show our file
    51.    
    52.     GetFileLength = bytesreadint
    53. End Function

    I've checked it against the inet.getheaders one, and the result is the same.

    Rate me if it works, Thanks
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  11. #11

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Ah, sorry for making you work for no reason. I can read the file and get its length fine. The goal is getting the length without actually getting the file.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  12. #12
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    Not all servers will supply a content length in the headers ...

    This gets the Headers ..
    objHTTP.getAllResponseHeaders

    This only gets it if the header type exists ..
    objHTTP.getResponseHeader("Content-Length")

    otherwise you need to read the file then get the length ..
    Last edited by rory; Jul 1st, 2006 at 10:00 PM.

  13. #13
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    Code:
    <%@ Language="VBScript" %> 
    <%
    Option Explicit
    
    Response.Buffer = True
    Response.Expires = 0
    Response.ExpiresAbsolute = Now() - 1
    Response.AddHeader "cache-control","private"
    Response.AddHeader "pragma", "no-cache"
    
    Dim objHttp
    Dim strURL
    Dim strStatus
    Dim strResults
    Dim strLength 
    
    strURL = "http://www.somelink.com"
    
    '// GET A REMOTE WEB PAGE
    Function RemoteData()
    	Dim strData
    	On Error Resume Next
    	Set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
    	objHttp.Open "GET", strURL, False
    	objHttp.setRequestHeader "Content-Type", "text/html"
    	If Err = 0 Then
    		objHttp.send
    		strStatus = objHttp.Status
    		strResults = objHttp.StatusText
    		If objHTTP.getResponseHeader ("Content-Length") = "" Then
    			Do Until objHTTP.readyState = 4: DoEvents: Loop
    			strData = objHttp.ResponseText
    			strData = Len(strData)
    		Else
    			strData = objHTTP.getResponseHeader("Content-Length")
    		End If
    	End If
    	Set objHttp = Nothing
    	If Err <> 0 Then
    		strData = "Error " & Err.Number & "<br>" & _
    		Err.Description & vbCrLf
    	End If	
    	RemoteData = strData
    End Function
    
    '// GET PAGE NOW
    strLength = RemoteData
    
    '// RESPONSE ERROR
    If strStatus >= 400 And strStatus <= 500 Then
    	Response.Write "Error " & strStatus & "<br>"
    	Response.Write strResults & vbCrLf
    	Response.End	
    '// SHOW DATA FROM PAGE
    Else
    	Response.Status = "200 Ok"
    	Response.Write "Content Length: " & strLength
    End If
    %>

  14. #14

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Well, that still doesn't do anything different from what I was doing. That site isn't sending Content-Length, so the function would get teh data every time which, unfortunately, isn't acceptable in my case.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  15. #15
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    If it doesnt send you the content length .. even if using Winsock .. there is no way to get it .. other than grabbing the resulting HTML/XML and getting the length of that ..

    unless you run a remote ASP page on that server to get the File size through FSO ... if its not your server though, then you cant do that ..

    Rory

    PS. I did add in Error Checking and Status Reponses .. LOL .. ;-)
    Last edited by rory; Jul 2nd, 2006 at 01:33 AM.

  16. #16
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    also, you are using MSXML anyway, you have to connect to it .. so why not just use the text it sends back to get the content length ..?? You have to connect to the webpage no matter what ... and with that comes a reply ..

  17. #17

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Well, I was posting in hopes of there being a way to force the response to contain a Content-Length or somesuch in a HEAD request. Some of the pages I'm getting are quite big, up to half an MB, and I would like to cache them, which is why I need the filesize. And before you ask, If-Modified-Since doesn't work either.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  18. #18
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    i tried it even with Winsock in VB and still no go .. most web pages just aren't sending a content length :-(

    Rory

  19. #19

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Oh well, thanks for trying. I'll see if I can find some workaround.
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

  20. #20
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP Remote file size

    Quote Originally Posted by Max_aka_NOBODY
    Well, I was posting in hopes of there being a way to force the response to contain a Content-Length or somesuch in a HEAD request. Some of the pages I'm getting are quite big, up to half an MB, and I would like to cache them, which is why I need the filesize. And before you ask, If-Modified-Since doesn't work either.
    .. ask them to use a database ..?

  21. #21

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: ASP Remote file size

    Quote Originally Posted by rory
    .. ask them to use a database ..?
    "Outside my competence."
    Please, put a checkmark ( ) or the word [RESOLVED] in your topic title if it was resolved, and rate the person who resolved it.

Posting Permissions

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



Click Here to Expand Forum to Full Width