2017-03-22 187 views
0

我有一個問題,我試圖連續下載兩個文件WebClient,第二個文件失敗。該文件位於運行IIS的127.0.0.2/files/中。目錄瀏覽已打開,我可以看到該位置的文件並通過瀏覽器下載。使用WebClient下載C#錯誤

FileDownload()WebClass在窗體加載時運行,併成功下載state.xml。我可以編輯這個文件,然後檢查下載目錄(應用程序路徑)文件已經改變。然後,表單加載運行ReadXml(),並且如果<action>true在此文件中,則下載與<file>一起運行,並且這是失敗的位置。我檢查目錄和文件不存在。 如果您在下載行中暫停程序,您將會看到第二次運行時出現未知WebException。

我不知道如何使這個問題更簡單,但我想我已經刪除了所有與問題無關的代碼。我通過幾個消息框來檢查進度,並且您將從中看到該文件位於網址上,但在本地找不到。我已經排除了文件夾的權限,因爲xml文件的下載質量很好,而且我最初使用的是一個批處理文件,只是簡單的壓縮文件,但切換到文本文件以防安全問題。

另外值得注意的是:當我暫停程序下載文件時,所有的變量都有正確的讀取並複製到線路client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile);中的鏈接到我的瀏覽器中,帶我到那個文件。

編輯:網絡例外:EX { 「一個Web客戶端請求期間發生異常。」} System.Net.WebException
的InnerException System.Exception的{{系統 「不支持給定路徑的格式。」}。 NotSupportedException異常}

下面是我的代碼:

Form1.cs的

namespace Test 
{ 
    private void button1_Click(object sender, EventArgs e) 
    { 
     WebClass.FileDownload(Globals.urlFiles + Globals.xmlPath, Globals.xmlPath); 
     Main.ReadXml(); 
    } 
} 

WebClass.cs

namespace Test 
{ 
    class WebClass 
    { 

     public static void FileDownload(string strDownUrl, string strSaveFile) 
     { 
      //var strDownUrl = Globals.url + Globals.xmlPath; 
      MessageBox.Show(strDownUrl); 
      var flag = 0; 
      System.Net.HttpWebRequest request = null; 
      System.Net.HttpWebResponse response = null; 
      request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strDownUrl); 
      request.Timeout = 30000; 
      try 
     { 
      response = (System.Net.HttpWebResponse)request.GetResponse(); 
      flag = 1; 
     } 
     catch 
     { 
      flag = -1; 
     } 

     if (flag == 1) 
     { 
      MessageBox.Show("File Found!"); 
     } 
     else 
     { 
      MessageBox.Show("File Not Found!"); 
     } 


     using (WebClient client = new WebClient()) 
     { 
      try 
      { 
       client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile); 
      } 
      catch (WebException ex) 
      { 
       var n = ex.Status; 
       MessageBox.Show("Cannot find " + strDownUrl); 
      } 

     } 
    } 
} 
} 

Main.cs

namespace Test 
{ 
    public static class Globals 
    { 
     private static string prUrl = "http://127.0.0.2"; 
     public static string url{ get { return prUrl; } set { prUrl = value; } } 

     private static string prUrlFiles = "http://127.0.0.2/files/"; 
     public static string urlFiles { get { return prUrlFiles; } set { prUrlFiles = value; } } 

     private static string prXmlPath = "state.xml"; 
     public static string xmlPath { get { return prXmlPath; } set { prXmlPath = value; } } 
    } 

    class Main 
    { 

     public static void ReadXml() 
     { 
      double xVersion; 
      bool xAction, xArchive; 
      string xFile; 

      XmlDocument doc = new XmlDocument(); 
      doc.Load(Application.StartupPath + @"\" + Globals.xmlPath); 

      //GET XML VALUES 
      xVersion = Convert.ToDouble(doc.SelectSingleNode("XML/Program/Version").InnerText); 
      xAction = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText); 
      xArchive = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText); 
      xFile = Convert.ToString(doc.SelectSingleNode("XML/Request/Command/File").InnerText); 

      if (xAction == true) 
      { 
       MessageBox.Show("Action"); 
       if (xFile != "") 
       { 
        WebClass.FileDownload(Globals.urlFiles + xFile, Application.StartupPath + @"\" + xFile); 
       } 
      } 
     } 
    } 

} 

state.xml

<?xml version="1.0" ?> 

<XML> 

    <Program> 
     <Version>0.1</Version> 
     <Action>True</Action> 
    </Program> 

    <Request> 
     <Number>1</Number> 
     <Serial_Number></Serial_Number> 
     <User></User> 
     <Command> 
      <File>test.txt</File> 
      <Archive>False</Archive> 
     </Command> 
    </Request> 

</XML> 
+0

所以當你解析從XML文件並運行WebClass.FileDownload您收到消息框,說: MessageBox.Show(「文件中找到!」) 我說得對不對? – anderhil

+0

這將是也很好武官什麼未知引發WebException說 – anderhil

+0

@anderhill你是正確的。編輯:除了內容 異常說不受支持的路徑格式,但我不能工作了,爲什麼因爲每個文件的路徑是相同的。 –

回答

2

我相信這個問題是本地文件路徑,而不是與服務器上的文件的問題。所以這裏採用的是文件路徑問題:

Application.StartupPath + @"\" + xFile 

我會強烈建議使用Path.Combine方法,並使用靜態Path類建築pathes工作。它知道如何自動處理斜槓。 所以,你的代碼應該是這樣的:

Path.Combine(Application.StartupPath,xFile) 

爲構建網址有幾個類

此外,如果有一些問題與Webclient,或任何System.Net命名空間的類,你可以打開更多的這裏描述的日誌記錄:

https://msdn.microsoft.com/en-us/library/bb203855.aspx

另外,我想重寫你的下載方法,看起來像這樣:

public static void FileDownload(string strDownUrl, string strSaveFile) 
    { 
     MessageBox.Show(strDownUrl); 
     System.Net.HttpWebRequest request = null; 
     System.Net.HttpWebResponse response = null; 
     request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strDownUrl); 
     request.Timeout = 30000; 
     try 
     { 
      response = (System.Net.HttpWebResponse)request.GetResponse(); 

      Debug.WriteLine(string.Format("Content length is {0}", response.ContentLength)); 
      Debug.WriteLine(string.Format("Content type is {0}", response.ContentType)); 

      using (var file = File.OpenWrite(strSaveFile)) 
      using (Stream stream = response.GetResponseStream()) 
      { 
       if (stream == null) 
       { 
        Debug.WriteLine("Response is null, no file found on server"); 
       } 
       else 
        stream.CopyTo(file); 
      } 
     } 
     catch(Exception e) 
     { 
      Debug.WriteLine("Error during copying:"+e); 
     } 

    } 

現在你可以看到究竟發生了問題,什麼是從服務器的響應,因爲你需要知道,如果它是在請求期間或寫入文件到本地文件系統中。