2017-03-22 121 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); 
     } 

    } 

现在你可以看到究竟发生了问题,什么是从服务器的响应,因为你需要知道,如果它是在请求期间或写入文件到本地文件系统中。