Re: Getting properties of file on web server without downloading it
- From: "Jwilliams" <NOjohnNOwilliams_NOesq@xxxxxxxxxxxxx>
- Date: Wed, 7 Feb 2007 18:27:00 -0000
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
.
- References:
- Getting properties of file on web server without downloading it
- From: Jwilliams
- Re: Getting properties of file on web server without downloading it
- From: Igor Tandetnik
- Getting properties of file on web server without downloading it
- Prev by Date: BEFORENAVIGATE2 doesn't fire in IE7
- Next by Date: How to download web page with image?
- Previous by thread: Re: Getting properties of file on web server without downloading it
- Next by thread: Re: Cannot access elements of form
- Index(es):
Relevant Pages
|