Re: Getting properties of file on web server without downloading it

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks, yes it's a HTTP server (Apache). The problem has 'developed' a bit,
along with a solution for the benefit of others.

I was issuing a HttpWebRequest for the file, however the
HttpWebResponse.ContentLength returned was -1. Curious about this, I added
some code to download the file and found that it's actually a HTML document
containing 'Permission Denied' text rather than the file (.pdf, .gif, .txt,
..anything) requested.

After a bit of research, this is caused by privacy and cookies. I can
simulate the problem in IE7 browser as follows: Log in to the web site
(http, not https), navigate to the page containing the http link of the file
I want to download, set Tools - Internet Options - Privacy to 'Block All
Cookies' and click on the link. This results in a web page saying
'Permission Denied'.

The Solution
-------------

My VB .Net program uses the AxWebBrowser control. In DocumentComplete, the
HTMLDocument Cookie property is a string containing my login name and a long
hex number string (possibly my password encrypted). Relevant bits of the
code to successfully download the file are:

AxWebBrowser1_DocumentComplete(....)

Dim mDoc as mshtml.HTMLDocument
Dim loginCookie as New Cookie
Dim i As Integer

'Create a Cookie object from the cookie string

mDoc = DirectCast(AxWebBrowser1.Document, mshtml.HTMLDocument)
i = Instr(mDoc.cookie,'=") 'cookie string only contains 1 name=value pair
loginCookie.Name = mDoc.cookie.Substring(0, i - 1)
loginCookie.Value = mDoc.cookie.Substring(i)
loginCookie.Domain = mDoc.domain

:

Dim webRequest as HttpWebRequest
Dim webResponse as HttpWebResponse
Dim cookies As New CookieContainer
Dim st As Stream

'Use the cookie in the request and download the file

cookies.Add(loginCookie)
webRequest = CType(webRequest.Create(sURL), HttpWebRequest)
webRequest.CookieContainer = cookies
webResponse = CType(webRequest.GetResponse(), HttpWebResponse)
st = webResponse.GetResponseStream()

'Read the stream and write to a new file


"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:e$cXWziSHHA.3592@xxxxxxxxxxxxxxxxxxxxxxx
Jwilliams <NOjohnNOwilliams_NOesq@xxxxxxxxxxxxx> wrote:
Is there any way of getting the file properties (size, creation date
etc.) of a file on a web server without downloading it?

Assuming it's on HTTP server, you can issue a HEAD request for the file.
Some, but not all, servers will respond with Content-Length: and
Last-Modified: headers.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925




.



Relevant Pages

  • Re: C# httpwebrequest cookie
    ... property on the HttpWebRequest to the CookieContainer class instance you ... > I am pretty new to network programming with c#, and is having problem> using cookie with HTTPWEBREQUEST object. ... My question involves sending> a request with cookie. ... What I am trying to do is to authenticate> myself in a browser, look at the HTTP header using a sniffer, and> simulate the http request but with different POST data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ticket disappears when browser is closed
    ... be careful - if the cookie is save to the users harddrive - anyone who has access to that directory can grab the cookie and bypass authentication. ... Dim New DatabaseUser ... Dim userData As String = DatabaseUser.UserData ... ByVal PersonID As Integer, ByVal roles As String) ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: DESPERATE: FormsAuthentication Problem
    ... > them in the forms authentication cookie. ... > ' Get ";" delimited string of the user's roles from the database ... > Dim authTicket As FormsAuthenticationTicket = New ...
    (microsoft.public.dotnet.framework.aspnet)
  • DESPERATE: FormsAuthentication Problem
    ... them in the forms authentication cookie. ... from the forms authentication cookie, create a new FormsIdentity object, ... ' Get ";" delimited string of the user's roles from the database ... Dim authTicket As FormsAuthenticationTicket = New ...
    (microsoft.public.dotnet.framework.aspnet)
  • DESPERATE: FormsAuthentication Problem
    ... them in the forms authentication cookie. ... from the forms authentication cookie, create a new FormsIdentity object, ... ' Get ";" delimited string of the user's roles from the database ... Dim authTicket As FormsAuthenticationTicket = New ...
    (microsoft.public.dotnet.framework.aspnet.security)