从网站下载文件

时间:2012-11-06 13:13:01

标签: c# .net httpwebrequest console-application

我必须从网站下载文件。我完成了身份验证部分。对于文件下载,我没有指向http://whatever.com/file.xlsx

等文件的链接

我有这样的网址:

http://whatever.com?b_action=cognosViewer&ui.action=view&ui.object=defaultOutput(CAMID(%22eBO%3au%3a11842%22)%2ffolder%5b%40name%3d%27My%20Folders%27%5d%2freport%5b%40name%3d%27AppointmentFacility%27%5d)&ui.name=AppointmentFacility&ui.format=spreadsheetML

这是我用来下载文件的代码

public void downloadFile(string url, string fileName)
{
    // Create a request using a URL that can receive a post. 
    request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.CookieContainer = cookieContainer;
    // Set the Method property of the request to GET.
    request.Method = "GET";
    // Get the response.
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(responseStream))
            {
                using (StreamWriter writer = new StreamWriter(fileName, false))
                {
                    writer.Write(reader.ReadToEnd());
                    writer.Flush();
                    writer.Close();
                }
            }
            responseStream.Close();
        }
        response.Close();
    }
}

但下载的文件只包含:

  

您的报告已准备就绪,并会在短时间内下载到您的网络浏览器中。

如果我将链接粘贴到Chrome中,它会显示一个网页:您的报告....然后开始下载。

非常感谢您的帮助!

更新: 我做了Chrome crtl + shift + i - >网络

报告的网址是: whatever.com/p2pd/servlet/dispatch/gd/CAFS3c00000054FAAAACBugIFfTwbt-vZG2RFR7xOa6-wlAebBE523zHThH-tlqz0TrwhgYlE_H4sIAAAAAAAAALMODfLxdNFQ8svyNPXNyq-wrQqt9A2JLPcNcaz0q-Kt8s*KNPGtcrRV0gQAGDKw6yoAAAA _ /

如果我登录ie并粘贴网址,则会显示防火墙错误。 正在查看会话信息以查看它将它们放在它没有的链接中。

这是heaeder信息:

GET / p2pd的/ servlet的/发送/ GD / CAFS3c00000054FAAAACBugIFfTwbt-vZG2RFR7xOa6-wlAebBE523zHThH-tlqz0TrwhgYlE_H4sIAAAAAAAAALMODfLxdNFQ8svyNPXNyq-wrQqt9A2JLPcNcaz0q-Kt8s * KNPGtcrRV0gQAGDKw6yoAAAA_ / HTTP / 1.1

主持人:whatever.com

连接:保持活力

User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.4(KHTML,与Gecko一样)Chrome / 22.0.1229.94 Safari / 537.4

接受:text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8

参考者:https://whatever.com/p2pd/servlet/dispatch

Accept-Encoding:gzip,deflate,sdch

接受语言:en-US,en; q = 0.8

Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3

Cookie:cam_passport = 101:1212028e-a300-b0b0-9a59-6b86b56842dd:2388506829; CEA-SSA = FALSE; usersessionid在= AQgAAAAZQaJQGUGiUAoAAADgMgQRkOeDfHCJFAAAACBugIFfTwbt / vZG2RFR7xOa6 / wlFAAAAAhIMT47wEMbibXlyTAd0JuxNeln; CRN = listViewSeparator%3Dnone%26displayMode%3Dlist%26timeZoneID%3DCST%26format%3DHTML%26automaticPageRefresh%3D30%26columnsPerPage%3D3%26productLocale%3Den%26useAccessibilityFeatures%3Dfalse%26showWelcomePage%3Dtrue%26showOptionSummary%3Dfalse%26showHiddenObjects%3Dfalse%26contentLocale%3Den% 26skin%3Dcorporate%26linesPerPage%3D15%26; userCapabilities = 7c6d%3B6f%3Bff071efa%26ARQAAAAgboCBX08G7f72RtkRUe8Tmuv8JQBBkqOE98nKAPC1mu7HFx2kwsKb; CAF = CAFW000000b0Q0FGQTNjMDAwMDAwM2FGQUFBQUNCdWdJRmZUd2J0LXZaRzJSRlI3eE9hNi13bFRtOGJUc0pHalZqSW1mSWZ0VHg3emhkKnpMOF8zNzU2MTJ8MTAxOjEyMTIwMjhlLWEzMDAtYjBiMC05YTU5LTZiODZiNTY4NDJkZDoyMzg4NTA2ODI5; cc_session = s_cc:| s_conf:NA | s_sch:TD | s_hd:SA | s_serv:NA | s_disp:NA | s_set:| s_dep:NA | s_dir:NA | s_sms:DD | s_ct:SA | s_cs:SA | s_so: SA | e_hp:CAMID(* 22eBO * 3AU * 3a11842 * 22)| e_proot:公共* 20Folders | prootid:i9E956E6B64DA4217A97F144A433887C1 | e_mroot:我* 20Folders | mrootid:iD973C48427A94A9AAFB17F9016C8A62A | e_mrootpath:CAMID(* 22eBO * 3AU * 3a11842 * 22)* 2ffolder * 5B * 40name * 3D * 27My * 20Folders * 27 * 5D | e_user:姓* 20LastName | CL:EN | DCID:i9E956E6B64DA4217A97F144A433887C1 | show_logon:假| UIG:| UI:|写:真| EOM:0 | PP: 2388506829 | cachestamp:2012-11-06T07:46:19; ARAHHC-EBO = ARAHHC-阿尔卡-eBO2

1 个答案:

答案 0 :(得分:0)

您可以使用firebug并查看从浏览器发出的请求。请注意,您认为创建报告的页面实际上是生成对报告文件本身的第二个请求。