简体   繁体   English

在C#中通过Javascript回发下载文件

[英]Download file by Javascript postback in C#

EDIT: I´ll be more specific. 编辑:我会更具体。 I want to do a script to download a group of files every day. 我想做一个脚本,每天下载一组文件。 To do this programmatically, i need to click in a javascript button. 要以编程方式执行此操作,我需要单击一个javascript按钮。 It´s simple when is just put the URL in WebRequest class, but in javascript button i don´t have the URL. 仅将URL放在WebRequest类中很简单,但是在javascript按钮中我没有URL。 How can i mount this URL? 如何安装此URL?

Request (by Fiddler): 请求(由Fiddler提供):

POST /SomeSite?Something.aspx HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://www.Site.com/Stackoverflow/SomeSite?Something.aspx
Accept-Language: pt-BR
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: www.Site.com
Content-Length: 10616
Connection: Keep-Alive
Pragma: no-cache
Cookie: idioma=pt-br; WT_FPC=id=187.16.81.13-3324702672.30186643:lv=1320587789589:ss=1320587578749

__EVENTTARGET=ctl00%24contentPlaceHolderConteudo%24lnkDownloadArquivo&__EVENTARGUMENT=&__VIEWSTATE=%BlaBlaBla

Here you can see the _EVENTTARGET that is using postback with a link Button which name is "lnkDownloadArquivo". 在这里,您可以看到_EVENTTARGET使用带有链接Button的回发,其名称为“ lnkDownloadArquivo”。 So far I understand you want to simulate same download request without button click. 到目前为止,我了解您想模拟相同的下载请求而无需单击按钮。 if so then you can check here a solution . 如果是这样,那么您可以在这里检查解决方案。 http://ciintelligence.blogspot.com/2011/01/fetching-aspnet-authenticated-page-with.html . http://ciintelligence.blogspot.com/2011/01/fetching-aspnet-authenticated-page-with.html here you can get idea how asp.net button post back request works. 在这里您可以了解asp.net按钮发回请求的工作方式。

The built-in class you need is the HTTPWebRequest (or WebRequest ) class. 您需要的内置类是HTTPWebRequest (或WebRequest )类。 To create one, call System.Net.WebRequest.Create() and pass your URL, add the appropriate headers using the Headers collection, write to the Stream retrieved from WebRequest.GetRequestStream() , then retrieve the response using WebRequest.GetResponse() . 要创建一个,请调用System.Net.WebRequest.Create()并传递您的URL,使用Headers集合添加适当的标题,写入从WebRequest.GetRequestStream()检索的Stream ,然后使用WebRequest.GetResponse()检索响应。 From the retrieved response object, you can get the response Stream using WebResponse.GetResponseStream() . 从检索到的响应对象中,可以使用WebResponse.GetResponseStream()获得响应Stream The Stream can then be read from like any other Stream object. 然后可以像其他任何Stream对象一样读取该Stream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM