Excel从ASP.net下载页面

我对networking编程相当陌生。 我试图从一个ASP.net网站下载一个excel。 IT不需要身份validation,但是我无法debuggingURL作为萤火虫不会如何请求的URL。 search高低。 任何投入这将是真棒。 先谢谢你。

url: http : //tebb.elpaso.com/ebbmasterpage/Capacity/OperAvailAutoTable.aspx?code=TGP&name=Operationally%20Available%20Capacity&status=Cap&sParam2=M&sParam3=5/4/2012&sParam8=S

操作:我点击“摘要下载”button下载excel。

我不知道onClick()去哪里。

http://tebb.elpaso.com/ebbmasterpage/Capacity/OperAvailAutoTable.aspx?code=TGP&name=Operationally+Available+Capacity&status=Cap&sParam2=M&sParam3=5%2f4%2f2012&sParam8=S

Param3代表date,%2f是一个html编码/

这里的关键是发布数据。 要下载excel文件,原始发布数据是:

__ EVENTTARGET =&_EVENTARGUMENT =&ctl00_WebSplitter1_tmpl0_NavigationUserControl1_TreeView1_ExpandState = ennunnnnnnnnunnnnnunnnnnnunnnnnnnnnnnnnnnnnnnnnnnnnnnunnnunnnnnnnnnnnnnnnnunnnnnnnn&ctl00_WebSplitter1_tmpl0_NavigationUserControl1_TreeView1_SelectedNode =&_ LASTFOCUS =&_VIEWSTATE =%2FwEPDwUIOTA0MDgxNzEPFgYeCmhpZGVDb2x1bW4y3QEAAQAAAP%2F%2F%2F %% 2F%2F%2F%2F %%%% 2BQFPcGVyQXZhaWxBdXRvVGFibGUuYXNweD9jb2RlPVRHUCZuYW1lPU9wZXJhdGlvbmFsbHkgQXZhaWxhYmxlIENhcGFjaXR5JnN0YXR1cz1DYXAmc1BhcmFtMT1UR1Amc1BhcmFtMj1EJnNQYXJhbTM9MDUvMDQvMjAxMiAwOTowMCZzUGFyYW00PVAwMDEwOCZzUGFyYW01PTAwMTc3NyZzUGFyYW02PU1MViA1MjkgLyBZc2Nsb3NrZXkgUGxhbnQgKFUvUyBTTSkmc1BhcmFtNz1NYXkgIDQgMjAxMiAgOTo0NlBNJnNQYXJhbTg9RCZkZXRhaWxzPVlkZAIKD2QWAgIBDw8WAh8nBfcBT3BlckF2YWlsQXV0b1RhYmxlLmFzcHg %%% 2BnL7w6b%2B8VpMMVQUpjRuWt1dkDI5QOKOHqk%3D&__ |上一页= q1Vu5vuap4CxPC3AZarvFeTSUmpu1Y5Qe6RAgsXMl -9eN -hzR45dQjqjC-HEjZ0&__ EVENTVALIDATION =%2FwEWDwKF0M%2FIBQKWk6OhBAKx%2FMC2DgLM5d7LCALhg%2FaUDwKHxPzXAQKkobflCwKtsbmoAQLQ5oWgCgL73d1IAvSyi6UMAv ayt6UMAvCyt6UMAoaSk6MKAqDJoG0zZk9itnjKzHvp455sJC0LF3pnU%2F9GwKhhL5KKketXYQ%3D%3D&ctl00_WebSplitter1_clientState =%7C0%7C216%2C2%2C1058%2C2%2C%7C%7C%5B%5B%5B%5Bnull%2Cnull%5D%5D%2C%5B%5D%2C%5B%7B %220%22%3A%5B%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C2%2Cnull%2Cnull%2Cnull%2C%2217%25%22%5D%5D%2C%221%22%3A%5B %5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%2283%25%22%5D%5D%7D%5D%5D%2C%5B%7B%7D%2C%5B %7B%222%22%3A%5B%221%22%2C10%2C%221058px%22%5D%2C%223%22%3A%5B%220%22%2C10%2C%22216px%22%5D%7D %5D%5D%2Cnull%5D&ctl00%24WebSplitter1%24tmpl1%24ContentPlaceHolder1%24sumBtnDownload =摘要+下载和ctl00%24WebSplitter1%24tmpl1%24ContentPlaceHolder1%24SumDownloadDDL = EXCEL&ctl00%24WebSplitter1%24tmpl1%24ContentPlaceHolder1%24PageSizeDDL = 15&ctl00xWebSplitter1xtmpl1xContentPlaceHolder1xUltraWebGrid1 =%253CDisplayLayout%253E%253CStateChanges%253E% 253C%2FStateChanges%253E%253C%2FDisplayLayout%253E&ctl00%24_IG_CSS_LINKS_ = MasterStyleSheet.css%7C ..%2Fig_res%2FElectricBlue%2Fig_sp litter.css%7C ..%2Fig_res%2FElectricBlue%2Fig_shared.css%7C ..%2Fig_res%2FElectricBlue%2Fig_grid.css

这是使用HTTP分析器v6发现的。 我做了一个VB6testing程序,我成功下载2012年5月4日,2012年5月3日和2012年5月5日,只需更改URL中的Param3,它使用cURL,但你应该能够使用它作为一个如何提交http请求来接收excel文件作为http响应的例子。 LoadResString(101)将上面的post数据加载到CURLOPT_POSTFIELDS中。 等同地,LoadResString可以被上面的post数据的string文字replace。

Private Sub XPButton1_Click() Dim CurlContext As Long Dim buf As New StringBuffer CurlContext = vbcurl_easy_init() vbcurl_easy_setopt CurlContext, CURLOPT_URL, "http://tebb.elpaso.com/ebbmasterpage/Capacity/OperAvailAutoTable.aspx?code=TGP&name=Operationally+Available+Capacity&status=Cap&sParam2=M&sParam3=5%2f3%2f2012&sParam8=S" vbcurl_easy_setopt CurlContext, CURLOPT_COOKIEJAR, App.Path & "\cookie.txt" vbcurl_easy_setopt CurlContext, CURLOPT_COOKIEFILE, App.Path & "\cookie.txt" vbcurl_easy_setopt CurlContext, CURLOPT_FOLLOWLOCATION, 1 vbcurl_easy_setopt CurlContext, CURLOPT_POST, 1 vbcurl_easy_setopt CurlContext, CURLOPT_POSTFIELDS, LoadResString(101) vbcurl_easy_setopt CurlContext, CURLOPT_PROXYAUTH, CURLAUTH_ANY 'the next 5 lines are only needed to accommodate connecting through a proxy server vbcurl_easy_setopt CurlContext, CURLOPT_HTTPPROXYTUNNEL, 0 vbcurl_easy_setopt CurlContext, CURLOPT_PROXY, "your.proxy.if.any.com" vbcurl_easy_setopt CurlContext, CURLOPT_PROXYPORT, 80 'or your proxy port vbcurl_easy_setopt CurlContext, CURLOPT_PROXYUSERPWD, "" vbcurl_easy_setopt CurlContext, CURLOPT_WRITEDATA, ObjPtr(buf) vbcurl_easy_setopt CurlContext, CURLOPT_WRITEFUNCTION, _ AddressOf WriteFunction vbcurl_easy_setopt CurlContext, CURLOPT_DEBUGFUNCTION, _ AddressOf DebugFunction vbcurl_easy_setopt CurlContext, CURLOPT_VERBOSE, True ret = vbcurl_easy_perform(CurlContext) Open App.Path & "\test.xls" For Output As #1 Print #1, buf.stringData Close #1 End Sub 

这是直接的方式来下载数据,使用Siddharth Rout的路线将是弹性的网站代码的变化。 关于“复杂部分”的最后一个注记。 所有你需要做的就是使用FindWindow等待“文件下载”框出现,一旦你知道它已经启动,你可以使用keybd_event发送3个标签和一个回车键,等待一秒钟,再次按下enter键和“y “如果你将覆盖文件。 现在,直接下载的好处是您可以精确地select在哪里以及如何保存它。

如果您想了解更多关于我的方法的详细信息,我将很高兴将代码发布到StringBuffer,WriteFunction和DebugFunction,并解释如何从vb引用libcurl。

更新,每个请求的附加代码: StringBuffer.cls

 Private byteData() As Byte Private stringLength As Long Private arrayLength As Long Private Sub Class_Initialize() ReDim byteData(1024) arrayLength = 1024 stringLength = 0 End Sub Public Sub Class_Terminate() Erase byteData End Sub Public Property Get stringData() As String stringData = String(stringLength, " ") CopyMemory ByVal stringData, byteData(0), stringLength End Property Public Property Let stringData(newStringdata As String) Dim newStringLength As Long newStringLength = Len(newStringdata) If newStringLength > arrayLength Then arrayLength = (arrayLength + (newStringLength - newStringLength Mod 2)) * 2 ReDim Preserve byteData(arrayLength) End If CopyMemory byteData(0), ByVal newStringdata, newStringLength stringLength = newStringLength End Property Public Function quickConcat(newStringdata As String) Dim newStringLength As Long newStringLength = Len(newStringdata) + stringLength If newStringLength > arrayLength Then arrayLength = (arrayLength + (newStringLength - newStringLength Mod 2)) * 2 ReDim Preserve byteData(arrayLength) End If Dim amountToAdd amountToAdd = newStringLength - stringLength CopyMemory byteData(stringLength), ByVal newStringdata, amountToAdd stringLength = newStringLength End Function 

WRITEFUNCTION:

 Public Function WriteFunction(ByVal rawBytes As Long, _ ByVal sz As Long, ByVal nmemb As Long, _ ByVal extra As Long) As Long Dim totalBytes As Long, i As Long Dim obj As Object, buf As StringBuffer Dim tempStr As String Dim Buffer() As Byte totalBytes = sz * nmemb Set obj = AsObject(extra) Set buf = obj ' append the binary characters to the HTML string If Not ((rawBytes = 0) Or (totalBytes = 0)) Then ReDim Buffer(0 To (totalBytes - 1)) As Byte CopyMemory Buffer(0), ByVal rawBytes, totalBytes tempStr = String(totalBytes, " ") CopyMemory ByVal tempStr, Buffer(0), totalBytes buf.quickConcat (tempStr) End If ' Need this line below since AsObject gets a stolen reference ObjectPtr(obj) = 0& ' Return value WriteFunction = totalBytes End Function 

DebugFunction:

 Public Function DebugFunction(ByVal info As curl_infotype, _ ByVal rawBytes As Long, ByVal numBytes As Long, _ ByVal extra As Long) As Long Dim debugMsg As String Dim i As Long debugMsg = "" For i = 0 To numBytes - 1 debugMsg = debugMsg & Chr(MemByte(rawBytes + i)) Next Debug.Print "info=" & info & ", debugMsg=" & debugMsg DebugFunction = 0 DoEvents End Function 

以下是在vb6中使用curl所需的dll和types库:vb6的libcurl绑定

将上面的文件解压缩到项目目录中,并转到project-> references并添加两个tlb文件(types库)。

另外,这里是vb libcurl绑定的sourceforge页面,下载包括源码和几个例子: http : //sourceforge.net/projects/libcurl-vb/

你正在尝试的是稍微复杂的。 不是第一部分,而是第二部分。

逻辑

第1部分 )单击“摘要下载”button启动“文件下载”对话框

第二部分 )点击“文件下载”中的保存button,最后保存文件


第1部分

将WebBrowser1控件和一个button放在窗体上。 粘贴这个代码。

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click WebBrowser1.Navigate("http://tebb.elpaso.com/ebbmasterpage/Capacity/OperAvailAutoTable.aspx?code=TGP&name=Operationally%20Available%20Capacity&status=Cap&sParam2=M&sParam3=5/4/2012&sParam8=S") End Sub Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As _ System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted If WebBrowser1.ReadyState <> 4 Then Exit Sub Dim el As HtmlElement = _ WebBrowser1.Document.GetElementById("ctl00_WebSplitter1_tmpl1_ContentPlaceHolder1_sumBtnDownload") el.InvokeMember("Click") End Sub 

现在当你运行这个代码的时候,它会点击“Summary Download”button并启动File Download对话框。 见下面的快照。


第2部分 – 复杂的部分

现在您需要使用API​​来单击“ File Download对话框上的“ Savebutton。 幸运的是,我已经在我的博客中介绍了这一点,所以你不会有任何问题:)

主题:VBA / VB.Net / VB6 – 单击IE下载窗口上的打开/保存/取消button – 第一部分

链接 : http : //www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/

快照

在这里输入图像说明

HTH