Re: download an image and save locally
From: Kevin Spencer (kspencer_at_takempis.com)
Date: 08/05/04
- Next message: Andreas Klemt: "Cannot redirect after http headers have been sent"
- Previous message: Matt: "Re: ASP.Net Web Page Busy"
- In reply to: kieran: "Re: download an image and save locally"
- Next in thread: kieran: "Re: download an image and save locally"
- Reply: kieran: "Re: download an image and save locally"
- Messages sorted by: [ date ] [ thread ]
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>
- Next message: Andreas Klemt: "Cannot redirect after http headers have been sent"
- Previous message: Matt: "Re: ASP.Net Web Page Busy"
- In reply to: kieran: "Re: download an image and save locally"
- Next in thread: kieran: "Re: download an image and save locally"
- Reply: kieran: "Re: download an image and save locally"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|