2016-08-03 161 views
0

嗨大家好我試圖寫一個遊戲修補程序,所以我正在下載一個文件,以找到我已經做了很多研究什麼代碼應該使用所有不同的方法結果在同一個WebClient下載文件工作不正確

  WebClient downloadClient = new WebClient(); 
      Uri url = new Uri("http://theblademasterrpg.com/Patcher/data/p.dat"); 
      downloadClient.DownloadFile(url, Application.StartupPath + "\\p2.dat"); 
      downloadClient.Dispose(); 
      FileStream file = new FileStream(Application.StartupPath + "\\p2.dat", FileMode.Open, FileAccess.Read); 

      string fileContents; 
      using (StreamReader reader1 = new StreamReader(file)) 
      { 
       fileContents = reader1.ReadToEnd(); 
      } 

文件似乎下載,但它不是糾正這種文件只有文字,它像1.0

它總是顯示這個不管我做什麼,甚至將其更改爲HTML文件

enter image description here

我不能幫助,但認爲這是值得的Web服務器相關的,所以我嘗試的Apache,而不是IIS具有相同的結果

有其他人expirenced這個我寧願下載HTTP和避免FTP

回答

0

該網站是不是去給那個容易的文件給你。您需要解析的初步反應了一下,你正在尋找這個字符串:

<frame src="http://71.82.23.25:8080/Patcher/data/p.dat" name="redir_frame" frameborder="0"> 

,然後在該src屬性中的URL再次但是現在稱之爲DownloadString

此代碼快速破解用字符串IndexOf找到該屬性。

using(WebClient downloadClient = new WebClient()) 
{ 
    Uri url = new Uri("http://theblademasterrpg.com/Patcher/data/p.dat"); 
    var html= downloadClient.DownloadString(url); 
    // parse to the frame src 
    var search = "<frame src=\""; 
    var pos = html.IndexOf(search); 
    // find the end quote 
    var lastQuote = html.IndexOf("\"", pos+ search.Length); 
    // download that src 
    var frameUrl = html.Substring(pos+ search.Length, lastQuote- (pos + search.Length)); 
    var real = downloadClient.DownloadString(frameUrl); 
    // real has now the content of that file 
    real.Dump(); // LinQPad 
} 

請注意,網站更改他們的html標記,您需要相應地適應此代碼。

+0

謝謝你我現在明白了,因爲我自己託管自己並掩蓋了我的網址,所以它不是正確的^。^謝謝你的回覆 – quatre432