Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to develop a method for my application to automatically download a data file from a state website. The state website link is: http://www.azliquor.gov/query/master.csv. When you enter this in your internet browser, a windwos pop-up is displayed asking if you you want to run it, save it, or cancel. If you pick "Save" (which is what I want to do) The the file dialog appears allowing you to select the location and/or change the file name.

What I want to do is automate this process.

I have tried using this code:

VB
Dim iAddr As String = "http://www.azliquor.gov/query/master.csv"
Dim iFIleName As String = WHSysLocalDir & "\Goverment\Arizona LL Master.csv"
System.Diagnostics.Process.Start(iSiteAddr, "Save " & iFileName)


But it still requires user interaction and it seems to ignore the second argument.

Any idea how to go about this?
Posted
Comments
Sergey Alexandrovich Kryukov 11-Feb-14 18:25pm    
There is no such concept as "run a Web application". (Sigh...) The question and this code make no sense at all.
—SA

What you write simply makes no sense. What do you think you are running when you start your Process? It depends on what the Shell would execute. Normally, it should be some Web browser, the one installed by default. On the user computer, such thing may or may not be installed, forget about functionality you need. How the is problem of downloading a file related to the browser?

Downloading a file is nothing but sending some HTTP request(s) and getting HTTP response(s).

So, you should use the class System.Net.HttpWebRequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].

You can look at my past answer where I provide a complete code of HTTP download: how to download a file from internet[^].

See also my past answers:
how to download a file from the server in Asp.net 2.0[^],
FTP: Download Files[^],
get specific data from web page[^],
How to get the data from another site[^].

See also: http://en.wikipedia.org/wiki/Web_scraping[^].

—SA
 
Share this answer
 
Comments
KevinBrady57 22-Sep-13 12:19pm    
Thank you very much for your response. This was all very helpful.
Sergey Alexandrovich Kryukov 22-Sep-13 12:24pm    
Very good, you are welcome.
Will you accept the answer formally then?
—SA
Here is the function I developed:

VB
Public Function DownLoadWebFile(ByVal iSiteAddr As String, ByVal iFileName As String) As Boolean

    DownLoadWebFile = False

    If IsPath(FilePathOnly(iFileName)) = False Then
        ' the path to the file does not exists
        Exit Function
    End If

    ' delete the existing file if it exists
    DeleteFile(iFileName)

    Try
        Dim iRequest As System.Net.HttpWebRequest
        Dim targetURI As New Uri(iSiteAddr)

        iRequest = DirectCast(System.Net.HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)

        If (iRequest.GetResponse().ContentLength > 0) Then

            Dim str As New System.IO.StreamReader(iRequest.GetResponse().GetResponseStream())

            File.WriteAllText(iFileName, str.ReadToEnd)

            str.Close()

            DownLoadWebFile = True

        End If

    Catch ex As System.Net.WebException
        'Error in accessing the resource, handle it
        MsgBox("ERROR! IOException " & vbCrLf & ex.ToString, , )

    End Try


End Function
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-14 18:26pm    
How can it be a solution, especially after my solution published a day ago?..
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900