![](/img/trans.png)
[英]HttpWebRequest/HttpWebResponse only works from Console app not ASP.NET
[英]Download a PDF from a third party using ASP.NET HttpWebRequest/HttpWebResponse
我想發送一個URL作為查詢字符串,例如
localhost/abc.aspx?url=http:/ /www.site.com/report.pdf
並檢測以上URL是否返回PDF文件。 如果它將返回PDF,則它將自動保存,否則會出錯。
有些頁面使用Handler來獲取文件,因此在這種情況下,我也想檢測並下載它們。
localhost/abc.aspx?url=http:/ /www.site.com/page.aspx?fileId=223344
以上內容可能會返回pdf文件。
捕獲此問題的最佳方法是什么?
謝謝
您可以像這樣下載PDF
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = req.GetResponse();
//check the filetype returned
string contentType = response.ContentType;
if(contentType!=null)
{
splitString = contentType.Split(';');
fileType = splitString[0];
}
//see if its PDF
if(fileType!=null && fileType=="application/pdf"){
Stream stream = response.GetResponseStream();
//save it
using(FileStream fileStream = File.Create(fileFullPath)){
// Initialize the bytes array with the stream length and then fill it with data
byte[] bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, bytesInStream.Length);
// Use write method to write to the file specified above
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
}
response.Close();
它可能來自.aspx處理程序這一事實實際上並不重要,它是所使用的服務器響應中返回的mime。
如果您正在獲得通用的mime類型,例如application / octet-stream,則必須使用更具啟發性的方法。
假設您不能簡單地使用文件擴展名(例如.aspx),則可以先將文件復制到MemoryStream(請參閱如何從.NET中的Stream獲取MemoryStream? )。 一旦有了文件的內存流,就可以對其進行“厚臉皮”查看(我說厚臉皮是因為這不是解析PDF文件的正確方法)
我不是PDF格式方面的專家,但是我相信使用ASCII讀取器讀取前5個字符會產生“%PDF-”,因此您可以通過
bool isPDF;
using( StreamReader srAsciiFromStream = new StreamReader(memoryStream,
System.Text.Encoding.ASCII)){
isPDF = srAsciiFromStream.ReadLine().StartsWith("%PDF-");
}
//set the memory stream back to the start so you can save the file
memoryStream.Position = 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.