C#使用WebClient下载时出错
我遇到一个问题,我试图使用C#使用WebClient下载时出错,c#,visual-studio,c#-4.0,C#,Visual Studio,C# 4.0,我遇到一个问题,我试图使用WebClient连续下载两个文件,但第二个文件失败。该文件位于127.0.0.2/files/,通过IIS运行。目录浏览已打开,我可以看到该位置的文件并通过浏览器下载它们 WebClass中的FileDownload()在加载表单时运行,并成功下载state.xml。我可以编辑此文件,然后在下载目录(应用程序路径)中检查该文件是否已更改。然后,表单加载将运行ReadXml(),如果此文件中为true,则下载将与一起运行,这就是下载失败的地方。我检查了目录,但文件不在那
WebClient
连续下载两个文件,但第二个文件失败。该文件位于127.0.0.2/files/,通过IIS运行。目录浏览已打开,我可以看到该位置的文件并通过浏览器下载它们
WebClass
中的FileDownload()
在加载表单时运行,并成功下载state.xml。我可以编辑此文件,然后在下载目录(应用程序路径)中检查该文件是否已更改。然后,表单加载将运行ReadXml()
,如果此文件中
为true
,则下载将与
一起运行,这就是下载失败的地方。我检查了目录,但文件不在那里如果在下载行暂停该程序,您将看到它第二次运行时出现未知的WebException。
我不知道如何使这个问题更简单,但我想我已经删除了所有与问题无关的代码。我有几个消息框来检查进度,您将从这些消息框中看到该文件在web地址上找到,但在本地找不到。我已经排除了文件夹权限,因为xml文件可以很好地下载,我最初使用的是一个批处理文件,普通的和压缩的,但为了防止安全问题,我切换到了文本文件
另外需要注意的是:当我在下载文件时暂停程序,所有变量都正确读取和复制了client.DownloadFile(strDownUrl,Application.StartupPath+@“\”+strSaveFile)行中的链接代码>进入我的浏览器会将我带到该文件
编辑:Web异常:ex{“WebClient请求期间发生异常。”}System.Net.WebException
InnerException{“不支持给定路径的格式。”}System.Exception{System.NotSupportedException}
下面是我的代码:
表格1.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.1
真的
1.
test.txt
假的
我认为问题在于本地文件路径,而不是服务器上的文件。因此,这里的文件路径存在问题:
Application.StartupPath + @"\" + xFile
我强烈建议使用Path.combined
方法,并使用静态Path
类构建路径。它知道如何自动处理斜杠。
因此,您的代码应该如下所示:
Path.Combine(Application.StartupPath,xFile)
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);
}
}
对于构建URL,有两个类
此外,如果Webclient
或任何System.Net
命名空间类出现一些问题,您可以打开此处描述的其他日志记录:
另外,我将重写您的下载方法,使其如下所示:
Path.Combine(Application.StartupPath,xFile)
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);
}
}
现在您可以看到问题发生的确切位置以及服务器的响应,因为您需要知道是在请求期间还是在将文件写入本地文件系统期间。我相信问题在于本地文件路径,而不是服务器上的文件问题。因此,这里的文件路径存在问题:
Application.StartupPath + @"\" + xFile
我强烈建议使用Path.combined
方法,并使用静态Path
类构建路径。它知道如何自动处理斜杠。
因此,您的代码应该如下所示:
Path.Combine(Application.StartupPath,xFile)
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);
}
}
对于构建URL,有两个类
此外,如果Webclient
或任何System.Net
命名空间类出现一些问题,您可以打开此处描述的其他日志记录:
另外,我将重写您的下载方法,使其如下所示:
Path.Combine(Application.StartupPath,xFile)
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);
}
}
现在您可以看到问题发生的确切位置以及服务器的响应,因为您需要知道是在请求期间还是在将文件写入本地文件系统期间。因此,当您从xml解析文件并运行WebClass.FileDownload时,您会收到一个消息框,上面写着:MessageBox.Show(“找到文件”);我说的对吗?如果能附上未知的WebException,那也很好says@anderhill你说得对。编辑:异常内容异常显示不支持的路径格式,但我无法找出原因,因为每个文件的路径都是相同的。因此,当您从xml解析文件并运行WebClass.FileDownload时,您会收到消息框:MessageBox.Show(“找到文件!”);我说的对吗?如果能附上未知的WebException,那也很好says@anderhill你说得对。编辑:异常内容异常显示不支持的路径格式,但我无法找出原因,因为每个文件的路径都相同。