使用ASP.net登录后获取文件到客户端计算机

时间:2014-02-05 12:28:51

标签: c# asp.net file remote-access

我有一个要求登录凭据的网址。输入正确的凭证后,.csv文件会自动下载到用户计算机。我在asp.net中编写了一个代码它下载文件但文件内容错误,显示了一些HTML内容。我正在使用此代码:

 string url = "https://www.abc.com/asdasdxcss/actionlist2.csv";
            string FSourceFilePath = url.Substring(url.LastIndexOf('/') + 1);
            WebRequest request = WebRequest.Create(url);
            request.Credentials = new NetworkCredential("temp", "test", "");
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string respurlpath = "";
            respurlpath = response.ResponseUri.AbsoluteUri;
            WebRequest request5 = WebRequest.Create(respurlpath);
            request5.Credentials = new NetworkCredential("temp", "test", "");
            HttpWebResponse response5 = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();

            int bytesRead; byte[] buffer = new byte[4 * 1024];
            bytesRead = dataStream.Read(buffer, 0, buffer.Length);

            Server.ScriptTimeout = 30 * 60;
            Response.Buffer = false;
            Response.BufferOutput = false;
            Response.Clear();
            Response.ContentType = "text/csv";
            Response.AppendHeader("Content-Disposition", "attachment; filename=Test.csv");
            if (response5.ContentLength != -1)
                Response.AppendHeader("Content-Length", response5.ContentLength.ToString());

            while (bytesRead > 0 && Response.IsClientConnected)
            {
                Response.OutputStream.Write(buffer, 0, bytesRead);
                bytesRead = dataStream.Read(buffer, 0, buffer.Length);
            }



            if (dataStream != null) dataStream.Close();
            Response.End();

我的代码下载的文件内容:

       <html>
    <head>
        <META HTTP-EQUIV="expires" CONTENT="0">
    </head>
    <body>
        <form method="POST" enctype="application/x-www-form-urlencoded" action="/nidp/saml2/sso?id=157&sid=0&option=credential&sid=0"></form>
        <script language="JavaScript">
            <!--
                document.forms[0].submit();
            -->
        </script>
    </body>
</html>

我的期望是:

parentID    Action_status   
123           True

0 个答案:

没有答案