简体   繁体   中英

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. It´s simple when is just put the URL in WebRequest class, but in javascript button i don´t have the URL. How can i mount this URL?

Request (by 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". 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 . here you can get idea how asp.net button post back request works.

The built-in class you need is the HTTPWebRequest (or WebRequest ) class. 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() . From the retrieved response object, you can get the response Stream using WebResponse.GetResponseStream() . The Stream can then be read from like any other Stream object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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