2016-09-18 164 views
0

我想從Asp.net中的下列網址下載文件https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/14333603_1289676231065862_2460190844802989442_n.jpg?oh=f6ccebc305e599e6dcc0bb7c994d8ac4&oe=586F4DD8 在img標籤中應用此URL作爲src工作正常,但我需要將此圖像保存在我的下載文件夾中。從cdn網址下載圖片文件

我試着WebClient.Download方法,Downloaddata和WebRequest的方法,但他們返回403 Forbidden錯誤

+0

讓我知道我的答案是否有效。 :) – Niklas

回答

2

嘗試使用WebRequest這樣的: (你必須指定相應的我想你的下載文件夾和鏈接)

string lnk = @"https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/14333603_1289676231065862_2460190844802989442_n.jpg?oh=f6ccebc305e599e6dcc0bb7c994d8ac4&oe=586F4DD8"; 
WebRequest request = HttpWebRequest.Create(lnk); 
((HttpWebRequest)request).AllowAutoRedirect = true; 
((HttpWebRequest)request).UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; 
int dataLength; 
int bytesRead; 
int wroteSoFar = 0; 
var response = request.GetResponse(); 
Stream responseStream = response.GetResponseStream(); 

byte[] buffer = new byte[1024]; 
using (FileStream oFile = new FileStream(Path.Combine(Environment.CurrentDirectory, @"img.jpg"), FileMode.Append, FileAccess.Write)) 
{ 
    dataLength = (int)response.ContentLength; 
    do 
    { 
     bytesRead = responseStream.Read(buffer, 0, buffer.Length); 
     oFile.Write(buffer, 0, bytesRead); 
     wroteSoFar += bytesRead; 
     //Console.WriteLine($"{(wroteSoFar/1000).ToString("N0")} KB/{(dataLength/1000).ToString("N0")} KB"); 
    } 
    while (bytesRead != 0); 
} 

我設法像這樣下載它。

+0

它很好。謝謝。 –