以编程方式运行Web应用程序 [英] Running Web application programmatically

查看:89
本文介绍了以编程方式运行Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序开发一种方法,以便从州网站自动下载数据文件。国家网站链接是:http://www.azliquor.gov/query/master.csv。当您在互联网浏览器中输入此内容时,会显示一个windwos弹出窗口,询问您是要运行,保存还是取消。如果您选择保存(这是我想要做的),将出现文件对话框,允许您选择位置和/或更改文件名。



我想做的是自动化这个过程。



我尝试过使用这段代码:



< pre lang =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。开始(iSiteAddr, 保存& iFileName)





但它仍然需要用户互动,它似乎忽略了第二个参数。



任何想法如何去做?

解决方案

你所写的内容毫无意义。当您启动流程时,您认为自己在运行什么?这取决于Shell将执行什么。通常,它应该是一些Web浏览器,默认情况下安装的浏览器。在用户计算机上,可能安装或不安装此类设备,忘记您需要的功能。如何下载与浏览器相关的文件?



下载文件只不过是发送一些HTTP请求并获得HTTP响应。



所以,你应该使用类 System.Net.HttpWebRequest

http://msdn.microsoft.com/en-us/library/system.net。 httpwebrequest.aspx [ ^ ]。



您可以查看我过去的答案,其中提供了完整的HTTP下载代码:如何从互联网上下载文件 [ ^ ]。



另见我过去的答案:

如何从Asp.net 2.0中的服务器下载文件 [ ^ ],

FTP:下载文件 [ ^ ],

从网页获取特定数据 [ ^ ],

如何从其他网站获取数据 [ ^ ] 。



另请参阅: http://en.wikipedia.org/ wiki / Web_scraping [ ^ ]。



-SA


这是我开发的功能:



 公共 功能 DownLoadWebFile( ByVal  iSiteAddr 作为 字符串 ByVal  iFileName  As   String  As  布尔 

DownLoadWebFile = False

如果 IsPath(FilePathOnly(iFileName))= False 然后
' 文件的路径不存在
退出 功能
结束 如果

' 删除现有文件(如果存在)
DeleteFile(iFileName)

尝试
Dim iRequest As System.Net.HttpWebRequest
Dim targetURI 作为 Uri(iSiteAddr)

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

如果( iRequest.GetResponse()。ContentLength> 0 然后

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

File.WriteAllText(iFileName,str.ReadToEnd)

str.Close()

DownLoadWebFile = 正确

结束 如果

Catch ex As System.Net.WebException
' 访问资源时出错,处理它
MsgBox( ERROR!IOException& vbCrLf& ex.ToString ,,)

End 尝试



结束 功能


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:

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?

解决方案

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


Here is the function I developed:

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


这篇关于以编程方式运行Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆