2016-09-16 79 views
0

我試圖從pastebin中獲取鏈接。鏈接是原始粘貼中唯一的文本。然後我想從pastebin中的鏈接下載一個文件。c#從pastebin獲取鏈接並從鏈接下載

WebRequest request = WebRequest.Create("http://pastebin.com/raw/Dtdf2qMp"); 

     WebResponse response = request.GetResponse();  

     System.IO.StreamReader reader = new 

     System.IO.StreamReader(response.GetResponseStream()); 

     Console.WriteLine(reader.ReadToEnd()); 

     WebClient client = new WebClient(); 

     client.DownloadFile (Link gotten from pastebin here, "c:\\File"); 

     System.Threading.Thread.Sleep(5000); 

回答

0

而不是將讀取的文本轉儲到控制檯輸出,您應該將其分配給一個變量。

var pastebinOutput = reader.ReadToEnd(); 

然後,只需將它作爲DownloadFile方法的鏈接即可。如果您想要驗證它實際上是從原始pastebin中獲得的URL,則可以查看System.Uri的TryCreate方法。