如何使用C#从HTML网页提交的POST表单下载PDF?

时间:2010-10-07 03:57:04

标签: c# .net javascript html post

我正在尝试使用C#windows窗体应用程序以编程方式下载一些PDF文档。现在,我已经足够为每个下载PDF的页面获取一个唯一的URL。

每个链接都是一个网页,一旦页面加载就通过POST提交表单

function window_onload() {
                Form1.submit();
            }

然后PDF开始下载。我想停止下载PDF并将其自动保存到我的本地计算机。我想这样做的原因是因为我每周需要下载大约15-20个PDF文件。

3 个答案:

答案 0 :(得分:1)

我会使用httpwebrequest对象。

取决于pdfs的大小和服务器的响应时间,您可以异步或同步地执行此操作。这是使用GetResponse()方法的同步风格。

void DoPDFDownload(string strMyUrl, string strPostData, string saveLocation)
{
    //create the request
    var wr = (HttpWebRequest)WebRequest.Create(myURL);
    wr.Method = "POST";
    wr.ContentLength = strPostData.Length;
    //Identify content type... strPostData should be url encoded per this next    
    //declaration
    wr.ContentType = "application/x-www-form-urlencoded";
    //just for good measure, set cookies if necessary for session management, etc.
    wr.CookieContainer = new CookieContainer();

    using(var sw = new StreamWriter(wr.GetRequestStream()))
    {
        sw.Write(strPostData);
    }

    var resp = wr.GetResponse();

    //feeling rather lazy at this point, but if you need further code for writing
    //response stream to a file stream, I can provide.
    //...

}

以下是一种可以复制/粘贴到LINQPad中的方法,以了解这些类的工作原理。

void DoSpeedTestDownloadFromFCC()
{

string strMyUrl = "http://data.fcc.gov/api/speedtest/find?latitude=38.0&longitude=-77.5&format=json";
    //create the request
    var wr = (HttpWebRequest)WebRequest.Create(strMyUrl);
    wr.ContentLength = strPostData.Length;
            //Note that I changed the method for the webservice's standard.
            //No Content type on GET requests.
    wr.Method = "GET";
    //just for good measure, set cookies if necessary for session management, etc.
    wr.CookieContainer = new CookieContainer();


    var resp = wr.GetResponse();

    //...
    using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
    {
                    //here you would write the file to disk using your preferred method
                    //in linq pad, this just outputs the text to the console.
        sr.ReadToEnd().Dump();
    }

}

答案 1 :(得分:0)

 class DownloadLibrary
 {
 public static string getContentType(string Fileext)
 {
string contenttype = "";
switch (Fileext)
{
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".doc":
contenttype = "application/msword";
break;
case ".ppt":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".jpg":
case ".jpeg":
contenttype = "image/jpeg";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".ico":
contenttype = "image/vnd.microsoft.icon";
break;
case ".zip":
contenttype = "application/zip";
break;
default: contenttype = "";
break;
}
return contenttype;
}

public static void downloadFile(System.Web.UI.Page pg, string filepath)
{
pg.Response.AppendHeader("content-disposition", "attachment; filename=" + new FileInfo(filepath).Name);
pg.Response.ContentType = clsGeneral.getContentType(new FileInfo(filepath).Extension);
pg.Response.WriteFile(filepath);
pg.Response.End();
}
}

参考文献:http://dotnetacademy.blogspot.com/2010/07/code-to-download-file-on-buttton-click.html

答案 2 :(得分:0)

Response.ClearContent();    
Response.ClearHeaders();    
Response.ContentType = "application/octet-stream";                            
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");    
Response.OutputStream.Write(data, 0, data.Length);    
Response.End();    

它会将给定filename的文件下载到您的本地磁盘。