Re: download an image and save locally

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

From: Kevin Spencer (kspencer_at_takempis.com)
Date: 08/05/04


Date: Thu, 5 Aug 2004 11:01:12 -0400

Okay, a web server is similar to a file server in that it can fetch files
for you, which is what you're trying to do. It is also like a file server in
that, in order to request a file, you have to know the file name. A file
server can return a listing of the contents of a directory, which is useful
for selecting files to request. A web server also has that capability,
although you can configure the web server to not allow directory browsing.
So, the solution to your problem lies in first determining whether or not
directory browsing is allowed.

If Directory Browsing IS allowed:
    Request the directory, and parse the response to get the file names of
all files in the folder. Identify the one with the naming convention you've
described, and download it by requesting it by name (URL)

If Directory Browsing IS NOT allowed:
    Set up a loop to attempt to download all combinations of possible file
names. Eventually, you'll hit the right one.

-- 
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"kieran" <kieran5405@hotmail.com> wrote in message
news:b39796b3.0408050627.4c8b3e65@posting.google.com...
> Hi,
>
> I now have the below code that will download an image from the web
> through my firewall and save it locally.  Great!
>
> However the image I want to download updates daily and conseqently its
> title changes daily.
>
> However only a certain part of the title changes. i.e. the image is
> desktop56247.gif. The image the next day will be desktop4112.gif.
> There is no other image on the page whose title starts with 'desktop'.
> Therefore, I need some way of saving an image at a particular url that
> has 'desktop' and '.gif' in its title.
>
>
> Any ideas and help much appreciated on how to figure this out.
>
>
>
>
>
> <code>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
>         Dim MyImage As System.Drawing.Image
>         Dim URL As String = "http://www.google.com/images/logo.gif"
>         Dim FileName As String, URLpieces As String()
>         URLpieces = Split(URL, "/")
>         FileName = URLpieces.GetValue(UBound(URLpieces))
>
>         MyImage = GetImage(URL)
>         MyImage.Save("D:\temp\image.gif")
>         MyImage = Nothing
>         Close()
>     End Sub
>
>     Function GetImage(ByVal URL As String) As System.Drawing.Image
>         Dim Request As System.Net.HttpWebRequest
>         Dim Response As System.Net.HttpWebResponse
>
>         Try
>             Request = System.Net.WebRequest.Create(URL)
>             Dim saByPassList() As String
>
>             Dim myProxy As New System.Net.WebProxy("10.32.0.20:8080",
> True, saByPassList, New System.Net.NetworkCredential("user",
> "password", "domain"))
>
>             myProxy.BypassProxyOnLocal = True
>
>             Request.Proxy = myProxy
>             Response = CType(Request.GetResponse,
> System.Net.WebResponse)
>             If Request.HaveResponse Then
>                 If Response.StatusCode = Net.HttpStatusCode.OK Then
>
>                     GetImage =
> System.Drawing.Image.FromStream(Response.GetResponseStream)
>
>                 End If
>
>             End If
>         Catch e As System.Net.WebException
>
>             MsgBox("A web exception has occured [" & URL & "]." &
> vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation,
> "Error!")
>             Exit Try
>
>         Catch e As System.Net.ProtocolViolationException
>
>             MsgBox("A protocol violation has occured [" & URL & "]." &
> vbCrLf & "  System returned: " & e.Message, MsgBoxStyle.Exclamation,
> "Error!")
>             Exit Try
>         Catch e As System.Net.Sockets.SocketException
>             MsgBox("Socket error [" & URL & "]." & vbCrLf & "  System
> returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
>             Exit Try
>         Catch e As System.IO.EndOfStreamException
>             MsgBox("An IO stream exception has occured. System
> returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
>
>             Exit Try
>         Finally
>         End Try
>     End Function
>
> </code>


Relevant Pages

  • Re: def-2001-32
    ... Open properties ... GH> - JRun 3.0 ... GH> Upon sending a specially formed request to the web server, ... GH> Directory Browsing Allowed set to FALSE. ...
    (Bugtraq)
  • Re: Read-only/hidden directories
    ... > An associate mentioned that I could further protect my private website by ... and disallowing visitors to browse my directories. ... To the folder that houses my web server? ... Other Web servers let you turn on directory browsing on ...
    (comp.security.firewalls)
  • Re: Read-only/hidden directories
    ... > An associate mentioned that I could further protect my private website by ... and disallowing visitors to browse my directories. ... To the folder that houses my web server? ... Other Web servers let you turn on directory browsing on ...
    (comp.security.firewalls)
  • Re: Application pool terminated unexpectedly
    ... Things that can be responding to the request include: ... pool 'DefaultAppPool' terminated unexpectedly. ... We have always used Nessus for this. ... condition where almost every scan I run against the rebuilt web server ...
    (microsoft.public.inetserver.iis)
  • Re: Suggestions on creating a File Download area
    ... > I'm in the process of setting up a web site that will allow users to ... > towards having a download area that users can log in to and simlply ... are outside the directories that the web server can access. ... or you could link the cleaning to logins. ...
    (alt.php)