您需要使用WebClient Credentials屬性設置您的憑據。您可以通過爲其分配NetworkCredential的實例來完成此操作。請看下圖:
using (WebClient client = new WebClient()){
client.Credentials = new NetworkCredential("user-name", "password");
client.DownloadFile("url", @"file-location");
}
編輯
如果你不想硬編碼用戶名和密碼,您可以在Web客戶端的UseDefaultCredentials屬性設置爲true。這將使用當前登錄用戶的憑證。從documentation。
Credentials屬性包含用於訪問主機上資源的認證憑證。在大多數客戶端場景中,您應該使用DefaultCredentials,這是當前登錄用戶的憑據。爲此,請將UseDefaultCredentials屬性設置爲true,而不是設置此屬性。
這將意味着你可以修改上面的代碼:
using (WebClient client = new WebClient()){
client.UseDefaultCredentials = true;
client.DownloadFile("url", @"file-location");
}
來源
2017-04-08 01:03:49
Dec
的URL重定向到登錄,您需要驗證這些參數傳遞在您下載圖片之前 – Krishna
我在網頁瀏覽器中登錄。 – szanex
不,你必須從網絡客戶端 – Krishna