如果有人將此網址https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06加載到瀏覽器中,則開始下載一個 excel 文件。所以當我通過HttpWebRequest調用相同的 url 時,excel 文件不會開始下載。我試過這個代碼示例。string address = "https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06";using (WebClient client = new WebClient()){ client.DownloadString(address);}我再次嘗試了這個。string url = "https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06";WebRequest request = HttpWebRequest.Create(url);WebResponse response = request.GetResponse();StreamReader reader = new StreamReader(response.GetResponseStream());string responseText = reader.ReadToEnd();但未能達到我的目標。代碼成功執行,但沒有開始下載我想要實現的 excel 文件。當我嘗試將此網址https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06加載到 webbrowser 控件中時,也看到了同樣的問題,沒有 excel 文件開始下載。這是我嘗試過的代碼。webBrowser1.Navigate("https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06");webBrowser1.ScriptErrorsSuppressed = true;我只是不明白為什么調用或執行相同的 url 時沒有下載 excel 文件。所以請有人告訴我我需要做什么,結果當我執行 url excel 文件將開始在客戶端 PC 中下載。請分享一些工作代碼示例。
1 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
DownloadString 將內容返回到內存中的變量中。文件不會保存在系統上。如果這是您想要的,那么您需要在代碼中進行一些小改動:
string address = "https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06";
using (WebClient client = new WebClient())
{
string contents = client.DownloadString(address);
}
變量“contents”將包含您問題中 URL 的 html。如果你想要它作為一個文件,那么我需要使用 DownloadFile 方法。電子表格本身是一個不同的 URL。
本文檔末尾有一個示例。
- 1 回答
- 0 關注
- 167 瀏覽
添加回答
舉報
0/150
提交
取消