[英]C# Error downloading with WebClient
我有一個問題,我嘗試使用WebClient
連續下載兩個文件,而第二個文件失敗。 該文件位於通過IIS運行的127.0.0.2/files/。 目錄瀏覽已打開,我可以看到該位置的文件並通過瀏覽器下載它們。
WebClass
FileDownload()
在加載表單時運行,並成功下載state.xml。 我可以編輯此文件,然后在下載目錄(應用程序路徑)中檢查文件是否已更改。 然后,表單加載將運行ReadXml()
,如果該文件中的<action>
為true
,則使用<file>
運行下載,這將導致下載失敗。 我檢查目錄,文件不存在。 如果您在下載行中暫停程序,則將在第二次運行該程序時看到未知的WebException。
我不確定如何使這個問題更簡單,但是我想我已經刪除了與該問題無關的所有代碼。 我在整個過程中都有幾個消息框,用於檢查進度,您會從中看到在Web地址上找到該文件,但在本地找不到該文件。 我已經排除了文件夾權限,因為xml文件下載得很好,並且最初使用的是一個批處理文件,該文件通常是純文本和壓縮文件,但萬一這是安全問題,則切換到文本文件。
還要注意:當我在該行暫停程序以下載文件時,所有變量在行client.DownloadFile(strDownUrl, Application.StartupPath + @"\\" + strSaveFile);
具有正確的讀取和復制鏈接client.DownloadFile(strDownUrl, Application.StartupPath + @"\\" + strSaveFile);
進入我的瀏覽器會將我帶到該文件。
編輯: Web異常:ex {“在WebClient請求期間發生異常。”} System.Net.WebException
InnerException {“不支持給定路徑的格式。”} System.Exception {System.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>
我相信問題出在本地文件路徑,而不是服務器上文件的問題。 因此,這里的文件路徑存在問題:
Application.StartupPath + @"\" + xFile
我強烈建議使用Path.Combine
方法,並使用靜態Path
類來構建路徑。 它知道如何自動使用斜杠。 因此,您的代碼應如下所示:
Path.Combine(Application.StartupPath,xFile)
對於構建URL,有幾個類
另外,如果Webclient
或任何System.Net
命名空間類存在某些問題,則可以打開此處描述的其他日志記錄:
https://msdn.microsoft.com/zh-CN/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);
}
}
現在您可以看到確切的問題發生在哪里以及服務器的響應是什么,因為您需要知道它是在請求期間還是在將文件寫入本地文件系統期間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.