有公共網站,我想以編程方式檢索一個文檔,通過瀏覽器完成工作,但通過代碼返回「無查詢」。通過httpwebrequest從網站檢索文檔,不會返回任何查詢
任何人都可以檢查出來,我在做什麼錯了,老老實實即時通訊上停留了幾天......
下面是代碼:
string fileUrl = @"http://docsonline.wto.org/imrd/directdoc.asp?DDFDocuments/t/G\SPS\NALB149.doc";
Uri uri = new Uri(fileUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
if (responseStream != null)
{
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
memoryStream.Seek(0, SeekOrigin.Begin);
string fileName = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1, fileUrl.Length - fileUrl.LastIndexOf("/") - 1).Replace(@"\", "_");
using (FileStream fileStream = System.IO.File.Create(@"C:\temp\" + fileName, (int)memoryStream.Length))
{
// Fill the bytes[] array with the stream data
byte[] bytesInStream = new byte[memoryStream.Length];
memoryStream.Read(bytesInStream, 0, (int)memoryStream.Length);
// Use FileStream object to write to the specified file
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
}
}
}
}
正如你可以看到有一個鏈接到我想要檢索的文件,任何人都可以嘗試一下嗎?
也許會有比我更幸運...
等待響應
鏈接重定向到登錄頁面。 –
@RichardDeeming,我不能告訴我重定向。 Chrome立即爲我開始下載。 –
然後,您可能已在Chrome中登錄該網站。嘗試與提琴手:http://www.fiddler2.com/fiddler2/ –